I wrote this regex with the idea that it will match any string in its entirety and simple return the whole string as the result.
The character class [^] containing the carrot asks for a match of all characters starting with anything exclude nothing. The * says let this happen any number of times.
But, my result matches the full string twice. I expect one match. What is wrong?
var regex = /([^]*)/;
var someString = "blahdy blah blah";
var result;
result = regex.exec(someString);
//why does this have a length of 2? I expect only 1 containing the entire string
console.log(result.length);
console.log(result);
You’re doing capturing (the
()inside the pattern), which means the regex call will return both the captured data (the whole string), as well as the entire string that caused the regex to match (again, the whole string).