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
2 Answers 2
First replace the more specialized one.
- Replace =)) with bla2
- 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
answered Apr 18, 2013 at 17:57
Jacob T. Nielsen
2,9786 gold badges28 silver badges30 bronze badges
Sign up to request clarification or add additional context in comments.
7 Comments
HTMHell
I can't do that. there are a lot of that example, and it'll take a lot of time.
Jacob T. Nielsen
what do you mean it will take a lot of time? It is 2 replace statements one preceding the other. Can you elaborate?
HTMHell
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 bigJacob T. Nielsen
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.
HTMHell
Look at my full code: pastebin.com/cU8mNFvi (Look at the second array, and the loop)
|
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
answered Dec 23, 2015 at 11:34
Dinesh Vaitage
3,2031 gold badge23 silver badges17 bronze badges
Comments
lang-js
=))first :)