如何在javascript中拆分字符串

时间:2009-09-29 15:46:43

标签: javascript string

  

可能重复:
  How do I split this string with JavaScript?

如何在javascript中拆分字符串?

示例str = "this is part 1 one wall this is part 2 "
现在我想将str分成2个部分,用单词wall

分隔

所以我希望输出为:

st1 ="this is part 1 "
st2 ="this is part 2 "

5 个答案:

答案 0 :(得分:15)

var str1 = "When I say wall I want to split";
var chunks = str1.split("wall");

alert(chunks[0]); /* When I say */
alert(chunks[1]); /* I want to split */

答案 1 :(得分:2)

为了完整起见,我将添加正则表达式答案:

var splits = str.split(/wall /);
alert("'"+splits[0]+"'"); //'this is part 1 '
alert("'"+splits[1]+"'"); //'this is part 2 '

答案 2 :(得分:1)

var parts = str.split('wall ');

var st1 = parts[0];
var st2 = parts[1];

答案 3 :(得分:1)

答案 4 :(得分:0)

var s = str.split("wall");
var st1 = s[0];
var st2 = s[1];