Reputation: 750
I have the following string of javascript:
#d1{width:100px;}
it looks like css code
now I wrote a regular expression that finds of an element starting with #
or .
in the string.
the regular expression is like this:
/(?:(#|\.)([A-Za-z0-1\-]+))/g
and the above regular expression matches successfully such strings like the above one.
but now when I tested this regular expression on the above string in Google console with then it returns false
the which I wrote was:
var str = "#d1{width:100px} " // returns undefined
var regex = / (?:(#|\.)([A-Za-z0-1\-]+)) /g //undefrined
regex.test(str) // returns false
Why it is not working?
Upvotes: 0
Views: 71
Reputation: 920
Try this. These spaces between / and parantheses are jerks.
function regtest()
{
var str = "#d1{width:100px}" // returns undefined
var regex = /(?:(#|\.)([A-Za-z0-1\-]+))/g //undefrined
alert(regex.test(str));
}
<button onclick="regtest()">Test</button>
Upvotes: 1
Reputation: 5347
You have spaces in your regular expression which you propably don't want there:
var str = "#d1{width:100px} " // returns undefined
var regex = /(?:(#|\.)([A-Za-z0-1\-]+))/g //undefrined
// ^-------------------------^----- see, no spaces here
regex.test(str) // returns true
Upvotes: 4