0

I want to remove the number and braces but splicing results in the string between the index taken from the string. I just want it removed leaving me with: "1234 ..... ABCDEFG"

 var mystring = "1234 ..... { 400 } ABCDEFG";

 var s1 = mystring.indexOf("{");
 var s2 = mystring.indexOf("}");        
 mystring = mystring.splice(s1,s2);

2 Answers 2

2

You can use a regex replace to just remove the braces and the chars between them:

var mystring = "1234 ..... { 400 } ABCDEFG";
mystring = mystring.replace(/\{.*?\}/, "");

Working demo: http://jsfiddle.net/jfriend00/sDHc9/


If you didn't want to use a regex, you could do it like this:

var mystring = "1234 ..... { 400 } ABCDEFG";
var s1 = mystring.indexOf("{");
var s2 = mystring.indexOf("}");   
mystring = mystring.substr(0, s1) + mystring.substr(s2 + 1);
alert(mystring);​

Working example: http://jsfiddle.net/jfriend00/RHhe4/

Sign up to request clarification or add additional context in comments.

Comments

1

splice is not a String method, so your code should throw a TypeError

use

var mystring = "1234 ..... { 400 } ABCDEFG";
mystring = mystring.replace(/{.+?}/,'');

Or, if you really want to use 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('');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.