1

I wanted to check the availability of multiple strings in a given string ( without using a loop ).

like

my_string = "How to find occurence of multiple sting in a given string using javascript RegExp";
// search operated on this string
// having a-z (lower case) , '_' , 0-9 , and space

// following are the strings wanted to search .( having a-z , 0-9 , and '_')

search_str[0]="How";
search_str[1]="javascript";
search_str[2]="sting";
search_str[3]="multiple";
  • I don't need their position.
  • I just needed to know all the search_str are must be in my_string.
  • order of search_str never effect the result .

is there is any regular expression available for this ?

UPDATE : WHAT AM I MISSING

in the answers i found this one is working in the above problem

if (/^(?=.*\bHow\b)(?=.*\bjavascript\b)(?=.*\bsting\b)(?=.*\bmultiple\b)/.test(subject)) {
    // Successful match
}

But in this case it is not working.

m_str="_3_5_1_13_10_11_";
search_str[0]='3';
search_str[1]='1';
tst=new RegExp("^(?=.*\\b_"+search_str[0]+"_\\b)(?=.*\\b_"+search_str[1]+"_\\b)");
if(tst.test(m_str)) alert('fooooo'); else  alert('wrong');

1 Answer 1

4
if (/^(?=.*\bHow\b)(?=.*\bjavascript\b)(?=.*\bsting\b)(?=.*\bmultiple\b)/.test(subject)) {
    // Successful match
}

This assumes that your string doesn't contain newlines. If it does, you need to change all the .s to [\s\S].

I have used word boundary anchors to make sure that Howard or resting don't accidentally provide a match. If you do want to allow that, remove the \bs.

Explanation:

(?=...) is a lookahead assertion: It looks ahead in the string to check whether the enclosed regex could match at the current position without actually consuming characters for the match. Therefore, a succession of lookaheads works like a sequence of regexes (anchored to the start of the string by ^) that are combined with a logical && operator.

Sign up to request clarification or add additional context in comments.

4 Comments

easy work around, just use [\S\s]* instead of .* - nvm. beat me to it. I'd suggest the //i switch so it's case insensitive.
my_string does not contain any new line and '.' only a-z(lower case) ,'_', and space. Thanks for the answer. Let me try it.
@ArjunRaj: \b only works at word boundaries, and there is no word boundary between 3 and _ (because both belong to the class of alphanumeric characters). Usually, you would fix this problem using lookaround assertions, but JavaScript doesn't support lookbehind. So you might need to tackle the problem in a different way, for example by first splitting the string along _, then checking the split elements. In any case, you need to define your problem more exactly (simplification is a surefire way to get a correct, but non-working answer). Better open a new question for this.
in that case I made a replacement -> '_' to space. ;)

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.