Maria
Maria

Reputation: 3525

Can I create a RegEx in Javascript that matches characters in order but not necessarily sequentially?

Does anyone know a way in Javascript to efficiently check if a string contains all the characters of a substring in order, even if they are not sequential (i.e. possibly separated by other content)?

Example:

hasChars("turtle", "I like tu uurtle"); // true
hasChars("turtle", "t,u,r,t,l,e,"); // true
hasChars("turtle", "turtel"); // false

Upvotes: 0

Views: 58

Answers (1)

vks
vks

Reputation: 67968

var patt = new RegExp(".*?t.*?u.*?r.*?t.*?l.*?e.*");
console.log(patt.test('I like tuuu ur,t,l,e,')); // true

Upvotes: 1

Related Questions