Reputation: 355
Can someone explain why this does not work? (I am using Chrome Developer Console)
pattern
-> "/Xmp\.MP\.RegionInfo\/MPRI:Regions/"
key
-> "Xmp.MP.RegionInfo/MPRI:Regions[1]"
key.search(pattern)
-> -1
key.search(/Xmp\.MP\.RegionInfo\/MPRI:Regions/)
-> -1
"Xmp.MP.RegionInfo/MPRI:Regions[1]".search(pat)
-> -1
"Xmp.MP.RegionInfo/MPRI:Regions[1]".search(/Xmp\.MP\.RegionInfo\/MPRI:Regions/)
-> 0
It make absolutely no sense to me that the search does not match if i use the variables....
Upvotes: 0
Views: 156
Reputation: 108480
It looks like pattern
is a String in your first example, it needs to be a RegExp
object:
var pattern = /Xmp\.MP\.RegionInfo\/MPRI:Regions/
var key = "Xmp.MP.RegionInfo/MPRI:Regions[1]"
key.search(pattern); // equals 0
If you want to convert a string to a regex, use the RegExp
constructor (but remove the slashes):
var pattern = new RegExp("Xmp\.MP\.RegionInfo\/MPRI:Regions");
Upvotes: 2
Reputation: 16130
In first case your pattern is wrapped in quotes, so it is string. In second case it is without quotes -> it is RegExp object.
Upvotes: 0
Reputation: 21091
In your example pattern appears to be a string. You need it to be a RegExp object.
Upvotes: 0