简体   繁体   中英

Javascript regExp - find brackets

I can't figure out how to search for a string containing something like "[1]", for some reason this doesn't work:

var regExp = '/\[[1-9]\]/';
var search = string.search(regExp); // returns -1

I've searched all over for a solution but can't find anything...

Try it without the '

var regExp = /\[[1-9]\]/;
var search = string.search(regExp);

I think it's the way you're actually attempting to match it. Try this:

string="something[1]";
if(string.match(/\[[1-9]\]/gi)) alert("Your string has brackets with a number inside!"); //Alerts correctly

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM