javascript - Regular expression to find a word at the end of the string -
i have string, based on end character of string need check condition. have managed write following script, not taking intended.
//var receivers_type = "d2845a48f91b29f9d0a6e96858f05d85xing"; var receivers_type = "d21fca8635940e97d0f8e132d806cedaxingpage"; if (/.*xing/.test(receivers_type)) { console.log('xing profile'); flag = 1; } if (/.*xingpage/.test(receivers_type)) { console.log('xing page'); flag = 0; }
when receivers_type = "d2845a48f91b29f9d0a6e96858f05d85xing"
condition working properly. enter first if loop. when receivers_type = "d21fca8635940e97d0f8e132d806cedaxingpage"
entering in both if cases. me solve issue.
you don't need .*
selector, should use folowing regexp
/xing$/ <-- character $ means xing must @ end of string
so code be:
//var receivers_type = "d2845a48f91b29f9d0a6e96858f05d85xing"; var receivers_type = "d21fca8635940e97d0f8e132d806cedaxingpage"; if (/xing$/.test(receivers_type)) { console.log('xing profile'); flag = 1; } if (/xingpage$/.test(receivers_type)) { console.log('xing page'); flag = 0; }
Comments
Post a Comment