1

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

1
  • 3
    Replace =)) first :) Commented Apr 18, 2013 at 17:57

2 Answers 2

1

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 
Sign up to request clarification or add additional context in comments.

7 Comments

I can't do that. there are a lot of that example, and it'll take a lot of time.
what do you mean it will take a lot of time? It is 2 replace statements one preceding the other. Can you elaborate?
I only gave an example. 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
Please see my updated example. You do not need to use a loop. If i misunderstand then please post some code. The problem you described getting "bla1)" seem to be you not replacing the most specialized one first.
Look at my full code: pastebin.com/cU8mNFvi (Look at the second array, and the loop)
|
0

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

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.