JavaScript拆分方法不起作用
我有一个奇怪的问题试图分割一个string,我分裂一次,并将其保存为一个variables,但后来我试图分裂我刚刚创build的值,我不能,它会抛出一个错误说:不能调用未定义的方法“拆分”
justName = fullLine.split('(', 1)[0]; dateOne = fullLine.split('(', 2)[1]; dateTwo = dateOne.split(')', 1); console.log(dateTwo);
如果我注销justName,那么没有问题。
fullLine的一个例子是:
In the Heat of the Night (1967)
我可以看到它的工作。 在这里检查这个jsFiddle。
<div id="a"></div> <input type="button" onclick="abc()" value="Split" /> function abc(){ var fullLine="hello(world)Qwerty"; justName = fullLine.split('(', 1)[0]; dateOne = fullLine.split('(', 2)[1]; dateTwo = dateOne.split(')', 1); alert(dateTwo); }
var str="In the Heat of the Night (1967)"; dateOne = str.split('(', 2)[1]; dateTwo = dateOne.split(')', 1); console.log(dateTwo);
它工作正常
这应该只发生在stringfullLine
不包含(
由于某种原因,因为然后通过split
返回的数组将不会有一个项目在位置1(因此将返回undefined
)。
有没有可能不是所有的数据都包含(
?