jQuery 如何使用jquery删除字符串中的一部分字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7030219/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to remove a part of string within a string using jquery
提问by Steven Zack
I'm developing a small app, when a checkbox is checked it's id value is added to a hidden field, when unchecked, it's id has to be removed from the hiddenfield value. I know how to add, but don't know how to remove. It's like say there is a string strValue="abcde"; how do I remove 'c' from the strValue using jQuery? Thank you.
我正在开发一个小应用程序,当一个复选框被选中时,它的 id 值被添加到一个隐藏字段中,当取消选中时,它的 id 必须从隐藏字段值中删除。我知道如何添加,但不知道如何删除。就像说有一个字符串 strValue="abcde"; 如何使用 jQuery 从 strValue 中删除“c”?谢谢你。
回答by Loktar
You can use replace.
您可以使用替换。
strValue.replace('c', '');
I would suggest two things, looking at jQuerys .Data() method for storing values, and at the very least making your list delimited by something such as a comma.
我会建议两件事,查看用于存储值的jQuerys .Data() 方法,并且至少让您的列表由逗号等分隔。
回答by ShankarSangoli
Try this, make sure you take the result of replace method into a new string. It does not modify the original string.
试试这个,确保将 replace 方法的结果转换为新字符串。它不会修改原始字符串。
var str = "somestring";
str = str.replace('some', 'someother');
回答by StevieG
If you know he string you're looking for... The easy way.
如果你知道他正在寻找你的字符串......简单的方法。
strValue.replace('c','')
Or you can use a regular expression:
或者您可以使用正则表达式:
strValue.replace(regexp,newstring)