HTMHell
HTMHell

Reputation: 6006

JS multiple replace

I have a problem.

I don't know how to explain it, so I'll give an example:

I want to replace =) with bla1, and I want to replace =)) with bla2.

But what happens is that =)) becomes bla1)

What can I do?

Thanks a lot, and sorry for my English

EDIT: I can't replace =)) first. I have more signs like this. (>:),:((,:)) and more...). All of them are in array and I use a loop to replace all of them. It'll be very complicated to change all of them. the Array is big

Upvotes: 1

Views: 391

Answers (2)

Dinesh Vaitage
Dinesh Vaitage

Reputation: 3183

You can try this For example...

If you want to replace multiple "-" from single string
var str = "only-for-test";
str.replace(/-/g, "");

//out put : onlyfortest

Upvotes: 0

Jacob T. Nielsen
Jacob T. Nielsen

Reputation: 2978

First replace the more specialized one.

  1. Replace =)) with bla2
  2. Replace =) with bla1

Example

var text = "This =)) is =) some demo =)) =) =)) text";
text = text.replace(/=\)\)/g, "bla2"); // =))
text = text.replace(/=\)/g, "bla1"); // =)

// text = This bla2 is bla1 some demo bla2 bla1 bla2 text

FAILED Example

var text = "This =)) is =) some demo =)) =) =)) text :))";
text = text.replace(/=\)/g, "bla1"); // =)
text = text.replace(/=\)\)/g, "bla2"); // =))

// text = This bla1) is bla1 some demo bla1) bla1 bla1) text 

Upvotes: 1

Related Questions