I've been having issues with replacements and javascript for the past week. I'm doing all sort of replacements with Python and so far I've never had so many issues. Please help me? :P
This is the part of my code that gives me problems:
function SaeReplacerMenuButton(buttonchosen){
var Texto = document.getElementById("wpTextbox1").value;
$("."+buttonchosen).each(function() {
var Reemplazo = this.value.split("=");
var PalabraAntigua = new RegExp(Reemplazo[0],'g'); /*Esto genera el regext*/
Texto = Texto.replace(" "+PalabraAntigua+" ", " "+Reemplazo[1]+" ");
Texto = Texto.replace(" "+PalabraAntigua+".", " "+Reemplazo[1]+".");
Texto = Texto.replace(" "+PalabraAntigua+",", " "+Reemplazo[1]+",");
}
);
document.getElementById("wpTextbox1").value = Texto;
}
It should:
- Get a string from specific fields (textareas with an specific class, actually). The value in one of those textareas is as example: "miedo={{miedo}}".
- Then split it into 2.
- Then do a search for the first split between whitespaces " miedo ".
- I finds any match, replace it for the 2nd split ( {{miedo}} ).
After that, I added some code to make replacementes if also finds it with a , and a . instead of just whitespaces, and that's when the problems starts. When I run the script, it replaces:
miedo miedo miedo miedo miedo miedo miedo miedo miedo miedo
with:
miedo {{miedo}} {{miedo}}.{{miedo}} {{miedo}}.{{miedo}} {{miedo}}.{{miedo}} {{miedo}}.{{miedo}} miedo
But why? there wasn't any dot in the original text, so the code that should replace it adding a dot shouldn't do anything.
Note: I know nothing about RegEx, so this was an attempt to do a global replacement with a var + whitespaces instead of just a string. Is there any "easy" way to do that?
I tried to replicate it on jsfiddle as seen at http://prntscr.com/7wwns1 but I was unable to make it works, so I decided to upload the script so you guys can test it.
It's a .rar file with a .js and a .html inside: http://coznothingisforever.webs.com/replacerforstackoverflow.rar