How can I modify this script so it checks and makes sure it contains the exact string "abc"?
(/^[a].*/i.test(mystring))
It needs开发者_开发百科 to be case insensitive and take lower and upper caps.
Though you can use regex, I feel this is better suited for .toLowerCase
& .indexOf
:
var hasABC = ("myabcstring".toLowerCase().indexOf("abc") !== -1);
Regex seems like overkill to me.
Answer to a comment:
var mystring = "FoOaBcBaR";
function checkForABC(){
if (mystring.toLowerCase().indexOf('abc') != -1)
alert('found ABC!');
else
alert('Did NOT find ABC');
}
checkForABC();
Demo
Just do this:
/^abc$/i.test(mystring);
If it's a simple check for a straight string, using something like
var exists = (mystring.toLowerCase().indexOf('abc') > -1);
To avoid excessive Regex usage as they usually command more resources.
think the regex expression your looking for is;
^abc$
Hope that helps.
精彩评论