开发者

String matching (Javascript)

开发者_运维百科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.


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

You can use it as follows

var pattern=/over/gi;
alert(errmsg.match(pattern));


The following test should solve your requirement:

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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜