RegEx in Node / Javascript - how to get pattern matching borders?

I am currently using regex in Javascript / Node via find (), and this works to find the beginning of the template. But I would also like to know where the pattern ends. Is it possible?

+4
source share
1 answer

If you use the RegExp.exec() method, you can get the necessary information.

 var pattern = /\d+\.?\d*|\.\d+/; var match = pattern.exec("the number is 7.5!"); var start = match.index; var text = match[0]; var end = start + text.length; 

/\d+\.?\d*|\.\d+/ equivalent to new RegExp("\\d+\\.?|\\.\\d+") . The literal syntax retains some backslashes.

+13
source

All Articles