admin管理员组文章数量:1394526
Relative newer to Javascript and a bit stuck with the following so would greatly appreciate some help...
I have a string made up of a list of categories and keywords which might appear like:
Category A:Keyword A, Category B:, Category C: Keyword B
The problem is displaying a category when there is no keyword - how can I do a Find and Replace to swap instances of :,
with just ,
?
I already use the following to insert a space after the ma:
cats = cats.replace(/,/g,", ");
but copying and modifying with the extra colon seems to break it...
Relative newer to Javascript and a bit stuck with the following so would greatly appreciate some help...
I have a string made up of a list of categories and keywords which might appear like:
Category A:Keyword A, Category B:, Category C: Keyword B
The problem is displaying a category when there is no keyword - how can I do a Find and Replace to swap instances of :,
with just ,
?
I already use the following to insert a space after the ma:
cats = cats.replace(/,/g,", ");
but copying and modifying with the extra colon seems to break it...
Share Improve this question edited Sep 3, 2012 at 12:39 Michael Berkowski 271k47 gold badges450 silver badges394 bronze badges asked Sep 3, 2012 at 12:37 neilneil 1,3062 gold badges12 silver badges21 bronze badges2 Answers
Reset to default 6Use:
cats = cats.replace(/:\s*,/g,", ");
I think you should use arrays:
var arr="Category A:Keyword A, Category B:, Category C:Keyword B".split(', ');
for(var i=0;i<arr.length;i++){
arr[i]=arr[i].split(':');
}
Then, arr
bees [["Category A", "Keyword A"], ["Category B", ""], ["Category C", "Keyword B"]]
本文标签: stringJavascript Replace colon and comma charactersStack Overflow
版权声明:本文标题:string - Javascript: Replace colon and comma characters - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744099861a2590825.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论