Tarek Elmadady
Tarek Elmadady

Reputation: 346

Widely Supported Regex look behind

Is There is any pattern equal to (?<=) or (?

Goal: capture specific middle of string in multi line mode. The Left or Right side can be only [a-zA-Z].

Note: (xx) is a specific characters!

Upvotes: 1

Views: 47

Answers (3)

Toto
Toto

Reputation: 91488

Use [a-zA-Z](xx)[a-zA-Z] and get group 1

var test = [
    'axxb',
    '2xxb',
    '@xxb',

];
console.log(test.map(function (a) {
  return a + ' :' + a.match(/[a-zA-Z](xx)[a-zA-Z]/);
}));

Upvotes: 1

Jack Bashford
Jack Bashford

Reputation: 44125

Just combine lookahead and lookbehind:

https://regex101.com/r/VTKSH8/1

The above captures the second x in the last two examples because they match - you can make the regex match two or more characters in the middle as well:

https://regex101.com/r/5ThvGd/1

Note: It's on Regex101 because it doesn't work on Stack Snippets

Upvotes: 0

U Rogel
U Rogel

Reputation: 1941

There is look-behind in js. Only problem is not supported in all browsers. Chrome yes, not sure about others. Also support in browsers is not covered by mdn

You can look on this mini example - should do what you want.

Upvotes: 0

Related Questions