2

I've seen multiple instance of that kind of question, but not the one I'm looking for specifically... (I just hope I'm not hopelessly blind ! :P)

Let's consider this code:

var oneString = "This is a string";
document.write(oneString.replace("is", ""));

I would have assumed that the output would have been:

This a string.

But this is the output I'm getting:

This a  string

It's like replace() think that the second argument sent is " " and not ""... What would be the proper manner then to strip the string of a given string, without having extra spaces floating in my output ?

2
  • 1
    Keep in mind that your expected output is actually: Th is a string. It will only replace once, and the first is it finds. Commented Feb 8, 2012 at 20:28
  • I didn't knew about that! I'll keep that in mind! Good thing, this was just for the example, on the real side, the seek after string is (and always will be) unique ! Commented Feb 8, 2012 at 20:35

5 Answers 5

3

You are actually getting "is" replaced with an empty string, it's the space before and after the "is" you replace that stay around as the two spaces you see. Try;

oneString.replace("is ", "")
Sign up to request clarification or add additional context in comments.

1 Comment

Oh my god. I feel SO stupid right now, this is indeed the case! I need to get some sleep... Thank you !
1

Are you sure you're not getting "This a string"?

I think you should replace "is " with "" to get your desired output. There is a space before as well as after the word.

Comments

1

Look at the original string - "This_is_a_string" (I replaced spaces with underscores). When you remove "is", you don't touch either of the surrounding spaces, so both end up in the output. What you need to do is oneString.replace("is","").replace(/ +/," ") -- get rid of "is" and then eliminate any double spaces. If you want to keep some double spaces, try oneString.replace(" is","") instead, though you will run into issues if the string starts with is (eg "is it safe?").

The best answer might be something like oneString.replace(/is ?/,"") to match is possibly followed by a space oroneString.replace(/ ?is ?/," ") to match is possibly surrounded by spaces, and replace all of them with one space.

1 Comment

This was indeed a problem of the surrounding spaces! Thanks !
1

You didn't include any spaces in your pattern. When I try your code in Chrome I get:

> "This is a string".replace("is","")
  "Th is a string"

One way to accomplish what you're trying would be to use a regexp instead:

> "This is a string".replace(/is\s/,"")
  "This a string"

Comments

1
var aString = "This is a string";
var find = "is";    // or 'This' or 'string'
aString = aString.replace(new RegExp("(^|\\s+)" + find + "(\\s+|$)", "g"), "$1");
console.log(oneString);

The only case where this isn't perfect is when you replace the last word in the sentence. It will leave one space at the end, but I suppose you could check for that.

The g modifier is to make the replace replace all instances, and not just the first one.

Add the i modifier to make it case insensitive.

If you also want this to work on strings like:

"This has a comma, in it"

Change the regexp to:

var find = "comma";
new RegExp("(^|\\s+)" + find + "(\\s+|$|,)", "g")

1 Comment

(I need one more reputation to give vote up! But you're absolutely right, as Joachim is too !)

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.