1

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

  1. "This" -- Valid
  2. "Th" -- Valid
  3. "This is" --Valid
  4. "This is Reg" -- Valid
  5. "This is Regex" -- Valid
  6. "Tis" -- InValid
  7. "is" -- InValid
  8. "Reg" -- InValid

So user can enter part of the Text or Full text. If user enters part of text, It should validate for sequence and if it doesn't matach with the txt sequence then its invalid.

I am looking for solution using jQuery, But even Plain JavaScript also fine.

Just to clarify my requirement I gave above 7 senarios, Its not that only user can enter above 7 (for Ex:- If user Enters "T", then it is alo valid as it is first Character and If user enters only "H" it is invalid, as it is not prceeded by "T"

1
  • 3
    jQuery has nothing to do with regex. Commented Sep 15, 2014 at 3:31

3 Answers 3

3

No need for regex, or even jQuery -- just test and see if whatever the user enters is a substring of your text:

function isMatch(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:

function isMatch(userTxt) {
    return txt.indexOf(userTxt) == 0;
}
Sign up to request clarification or add additional context in comments.

4 Comments

Consider userTxt = "This is Regexa"
@sahbeewah - what about it?
It should return invalid whilst your solution would return valid.
@sahbeewah -- Did you try running it? My solution returns false if I passed in "This is Regexa" since it isn't a substring of txt.
2

It seems like what you are looking for is txt.substring(0, subtxt.length) === subtxt. In other words, does the search string subtxt match the beginning of the txt string.

function match(txt, subtxt) {
    return txt.substring(0, subtxt.length) === subtxt;
}

Test

> txt = "This is Regex";
> ["This", "Th", "This is", "This is Reg", "This is Regex", "Tis", "is",   "Reg"]
    .forEach(function(sub) { 
        console.log('"'+sub+'"', "--", match(txt, sub) ? "Valid" : "InValid"); 
  })

"This" -- Valid
"Th" -- Valid
"This is" -- Valid
"This is Reg" -- Valid
"This is Regex" -- Valid
"Tis" -- InValid
"is" -- InValid
"Reg" -- InValid 

ES6's startsWith

In ES6, you could use the new String#startsWith method.

In the polyfill given in the above page, they suggest using indexOf, as in

function match(txt, subtxt) {
    return !txt.indexOf(subtxt);
}

and indeed that seems that it could be faster than extracting the leading part of the string and doing a full string comparison.

But I really wanted a regexp

If for some reason you really want to do this with a regexp, then try

var regexp = /^T(h(i(s( (i(s( (R(e(g(e(x)?)?)?)?)?)?)?)?)?)?)?)?$/

This regexp can be "read aloud" as

It starts with a 'T' followed by either nothing or an 'h' followed by either nothing or an 'i' followed by either nothing or an 's' followed by....".

You get the idea.

function match1(subtxt) {
    return regexp.test(subtxt);
}

Test

> ["This", "Th", "This is", "This is Reg", "This is Regex", "Tis", "is",   "Reg"]
    .forEach(function(sub) { 
        console.log('"'+sub+'"', "--", match1(sub) ? "Valid" : "InValid");  
  })

"This" -- Valid
"Th" -- Valid
"This is" -- Valid
"This is Reg" -- Valid
"This is Regex" -- Valid
"Tis" -- InValid
"is" -- InValid
"Reg" -- InValid 

Creating the regexp programatically

If you want to automatically turn your string into a regexp which behaves the way you want, although this seems completely pointless, you could do

function make_regexp(str) {

    function _make_regexp(str) {
        return str && str[0] + "(" + _make_regexp(str.slice(1)) + ")?";
    }

    return RegExp("^" + _make_regexp(str) + "$");
}

> make_regexp("This is Regex")
/^T(h(i(s( (i(s( (R(e(g(e(x()?)?)?)?)?)?)?)?)?)?)?)?)?$/

7 Comments

The RegExp version does not anchor the expression to the start of the string.
Also, the make_regexp function would be much more efficient without recursion. Consider using split and join: return str.split('').join('(') + (new Array(str.length).join(')?'));
@torazaburo: But this match also "T" and other combination
@walidtoumi What other combination does it match?
@torazaburo: it match: Thi This i This is R This is Re
|
0

Try with this

 ^Th(?:is is Reg(?:ex)?|is(?: is)?)?$

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.