admin管理员组文章数量:1352788
var temp = "/User/Create";
alert(temp.count("/")); //should output '2' find '/'
i will try this way
// the g in the regular expression says to search the whole string
// rather than just find the first occurrence
// if u found User -> var count = temp.match(/User/g);
// But i find '/' char from string
var count = temp.match(///g);
alert(count.length);
u can try here /
var temp = "/User/Create";
alert(temp.count("/")); //should output '2' find '/'
i will try this way
// the g in the regular expression says to search the whole string
// rather than just find the first occurrence
// if u found User -> var count = temp.match(/User/g);
// But i find '/' char from string
var count = temp.match(///g);
alert(count.length);
u can try here http://jsfiddle/pw7Mb/
Share Improve this question asked Jul 26, 2012 at 6:16 SenderSender 6,85812 gold badges49 silver badges69 bronze badges 02 Answers
Reset to default 4You would need to escape the slash in regex literals:
var match = temp.match(/\//g);
// or
var match = temp.match(new RegExp("/", 'g'));
However, that could return null
if nothing is found so you need to check for that:
var count = match ? match.length : 0;
A shorter version could use split
, which returns the parts between the matches, always as an array:
var count = temp.split(/\//).length-1;
// or, without regex:
var count = temp.split("/").length-1;
Enter a regular expression using the escape character: (\)
var count1 = temp1.match(/\//g);
本文标签: javascriptcount special characters from string in jqueryStack Overflow
版权声明:本文标题:javascript - count special characters from string in jquery - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743863223a2552125.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论