【发布时间】:2013-10-26 18:38:47
【问题描述】:
希望是一个基本的问题,因为我有点迷失从哪里开始。
假设我们在 JS 中有这个字符串:
var foo = “是的,今天#hello #vcc #toomanyhashtags #test 我们去公园跳舞了”
我将如何动态查找字符“#”并删除之后的所有内容,直到它碰到一个空格(或在 #test 的实例中,直到字符串结束) em>?
即。所以上面的字符串会这样写:
var foo = "是的,今天我们去公园跳舞了"
我的概念是循环遍历整个字符串的字符和if 字符 === "#",删除字符直到当前循环的项目 === ""。有没有更短的方法来做到这一点?
初步概念:
var foo = "Hello, this is my test #test #hello";
var stripHashtags = function(x) {
for (i=0; i < x.length; i++) {
if (x[i] !== "#") {
console.log(x[i]);
}
}
};
stripHashtags(foo);
【问题讨论】:
标签: javascript jquery string substring splice