我想删除数字和大括号,但拼接会导致从字符串中获取的索引之间的字符串。我只想将其删除,留下:“1234 ..... ABCDEFG”
var mystring = "1234 ..... { 400 } ABCDEFG";
var s1 = mystring.indexOf("{");
var s2 = mystring.indexOf("}");
mystring = mystring.splice(s1,s2);
我想删除数字和大括号,但拼接会导致从字符串中获取的索引之间的字符串。我只想将其删除,留下:“1234 ..... ABCDEFG”
var mystring = "1234 ..... { 400 } ABCDEFG";
var s1 = mystring.indexOf("{");
var s2 = mystring.indexOf("}");
mystring = mystring.splice(s1,s2);
您可以使用正则表达式替换来删除大括号和它们之间的字符:
var mystring = "1234 ..... { 400 } ABCDEFG";
mystring = mystring.replace(/\{.*?\}/, "");
工作演示:http: //jsfiddle.net/jfriend00/sDHc9/
如果你不想使用正则表达式,你可以这样做:
var mystring = "1234 ..... { 400 } ABCDEFG";
var s1 = mystring.indexOf("{");
var s2 = mystring.indexOf("}");
mystring = mystring.substr(0, s1) + mystring.substr(s2 + 1);
alert(mystring);
工作示例:http: //jsfiddle.net/jfriend00/RHhe4/
splice
不是一种String
方法,所以你的代码应该抛出一个TypeError
采用
var mystring = "1234 ..... { 400 } ABCDEFG";
mystring = mystring.replace(/{.+?}/,'');
或者,如果你真的想使用splice
var mystring = "1234 ..... { 400 } ABCDEFG";
var s1 = mystring.indexOf("{");
var s2 = mystring.indexOf("}");
mystring = [].splice.call(mystring,0,s1)
.concat([].splice.call(mystring,s2+1))
.join('');