I'm using the following code to search sub string in a string
mystring.search(new RegExp(substring, 'i'))
Reason why I am using new RegExp
is, I want to search case insensitive. However, when there is a string like
var mystring = '10" stick';
and I want to search 10"
, the code above does not return any result. It's clearly because of new RegExp
and double quote. Is there any particular flag that needs to be passed in new RegExp
? I googled a lot but couldn't find any solution. What am I missing?
search
returns match position, maybe ur confused by this.
So, try this out,
const myString = '10" stick';
console.log(myString.match(new RegExp('10"', 'i'))[0])
console.log(myString.match(new RegExp('ick', 'i'))[0])
console.log(myString.match(new RegExp('asd"', 'i'))) // non-matching
It returns the match or null
if theres non. AND it matches 10"
in 10" stick