Regular Expression To Match All Or Initial Part Of A String In Javascript
I Am trying to find out a Regular expression, which will match with below criteria. My String to Match var txt = 'This is Regex'; User can enter anything like below 'This' -- Va
Solution 1:
No need for regex, or even jQuery -- just test and see if whatever the user enters is a substring of your text:
functionisMatch(userTxt) {
return txt.indexOf(userTxt) != -1;
}
(If the user text doesn't exist in the string, then calling indexOf
will return -1 since the substring doesn't exist in the string.)
If the string the user enters must start at the beginning of txt
, you could more explicitly check if the index returned is exactly 0:
functionisMatch(userTxt) {
return txt.indexOf(userTxt) == 0;
}
Solution 2:
Try with this
^Th(?:is is Reg(?:ex)?|is(?: is)?)?$
Post a Comment for "Regular Expression To Match All Or Initial Part Of A String In Javascript"