0

I have the following javascript functions:

function fun1()
{
...
return 'Over';
}
function fun2()
{
...
return 'Hello';
}
function fun3()
{
..
return 'Over';
}

function fun4()
{
..
return 'Over';
}
function fun5()
{
...
return 'Bye';
}
....
var errmsg='';

errmsg+=fun1();
errmsg+=fun2();
errmsg+=fun3();

var newmsg = errmsg;

if (newmsg.match('Over'))
{
checklist();
}

so as per the code, if fun1() and fun2() are true, errmsg= 'OverHello', and if fun1() and fun3() are true, errmsg = 'OverOver'

I want newmsg to match ONLY with the functions that return 'Over'...so if errmsg='Over' or 'OverOver' or so on, ONLY then it would run the function checklist().

Is there any way, I can check this condition using string methods (like match, contains, etc)?

Please help.

0

3 Answers 3

2

There is a match() function for string objects in javascript.

You can use it as follows

var pattern=/over/gi;
alert(errmsg.match(pattern));
Sign up to request clarification or add additional context in comments.

3 Comments

Thx Ammu. But this function is similar to what I have already used in my code.:)
Actually we are passing a regular expression to the match() function so that it can return all the occurrence of "over" in the errmsg. Just try out this snippet. var str = "HelloOver"; alert(str.match(/over/gi));//This will alert over---- alert(str.match(over));//This will return nothing
Ah yes! You are right.I tried it out and it works like a charm! Thx Ammu
1

The following test should solve your requirement:

errmsg.match(/^(Over)+$/)

1 Comment

+1 I forgot the ^ & $ in my answer. Note, however that this will return the matched string, not true/false.
0

Maybe you can replace all 'Over' text in the string. If any text is left after that operation, you string is containing something else than 'Over' and your check returns false

2 Comments

Can you please tell me how I can use the replace function in this case? Thx
@Gin: try str.replace(/over/gi, "")

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.