How to use the exec function from xregexp

Find comprehensive JavaScript xregexp.exec code examples handpicked from public code repositorys.

17
18
19
20
21
22
23
24
25
26
var date = XRegExp('(?<year>  [0-9]{4} ) -?  # year  \n\
                    (?<month> [0-9]{2} ) -?  # month \n\
                    (?<day>   [0-9]{2} )     # day   ', 'x');

// XRegExp.exec gives you named backreferences on the match result
var match = XRegExp.exec('2012-02-22', date);
match.year; // -> '2012'

// It also includes optional pos and sticky arguments
var pos = 3, result = [];
fork icon90
star icon165
watch icon25

+ 3 other calls in file

23
24
25
26
27
28
29
30
31
32
match.year; // -> '2017'

// It also includes optional pos and sticky arguments
let pos = 3;
const result = [];
while (match = XRegExp.exec('<1><2><3>4<5>', /<(\d+)>/, pos, 'sticky')) {
    result.push(match[1]);
    pos = match.index + match[0].length;
}
// result -> ['2', '3']
fork icon1
star icon1
watch icon3

+ 3 other calls in file

24
25
26
27
28
29
30
31
32
```js
// Change this
const name = XRegExp.exec(str, regexWithNamedCapture).name;

// To this
const name = XRegExp.exec(str, regexWithNamedCapture).groups.name;
```

See below for more examples of using named capture with `XRegExp.exec` and `XRegExp.replace`.
fork icon286
star icon0
watch icon71

+ 7 other calls in file

21
22
23
24
25
26
27
28
29
30
const r = '^\\s*@(?P<fnType>(mixin|function))\\s+' +
      '(?P<name>' + this.settings.fnIdentifier + ')' +
      '\\s*(?:\\(\\s*(?P<args>.*?)\\)|{)';
const regex = xregexp(r);

const matches = xregexp.exec(line, regex);
if (matches === null) { return null; }

return [
  matches.name,
fork icon93
star icon405
watch icon10

+ 5 other calls in file

3
4
5
6
7
8
9
10
11
12
var date = XRegExp(`(?<year>  \\d{4} ) -?  # year 
                    (?<month> \\d{2} ) -?  # month 
                    (?<day>   \\d{2} )     # day   `, 'x');

// XRegExp.exec gives you named backreferences on the match result
var match = XRegExp.exec('2015-02-22', date);
console.log(match); // -> [ '2015-02-22', '2015', '02', '22', index: 0, input: '2015-02-22', 
                    //       year: '2015', month: '02', day: '22' ]

// XRegExp.replace allows named backreferences in replacements
fork icon1
star icon0
watch icon2

+ 3 other calls in file

47
48
49
50
51
52
53
54
55
56

let message = {};
let regex = PATTERNS[type];

try {
  let match = xRegExp.exec(stripped, regex);
  let captureNames = regex.xregexp.captureNames;

  R.forEach(field => {
    if (captureNames.indexOf(field) >= 0) {
fork icon5
star icon0
watch icon1