In Javascript, how do I get the length of the regex match?
For example, if the string is
str = "/abc/hellothere/andmore/"
And the regexp is
reg = new RegExp('/abc/[^/]*/');
Then I want 16, the length of
/abc/hellothere/
Assuming you actually want your regex to match your sample input:
var str = '/abc/hellothere/andmore/';
var reg = new RegExp('/abc/[^/]*/');
var matches = str.match(reg);
if (matches && matches.length) {
console.log(matches[0].length);
}
The expected output should be 16
.
Refer to String.prototype.match
and RegExp.prototype.exec
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With