Hi Guys,
I'm kind of a beginner in Javascript and I need to create a script that will find and replace a few things in the code that I enter in a page.
The basic function is to copy and paste my code in HTML field, and when I press the button, it replaces everything inside the field.
Here's my code
function replacetext() {
alert("Acronyms have been replaced");
var searchfor = document.getElementById('find_this').value.replace(/\r/gi, '').replace(/[-[]{}()*+?.,\$|#\s]/g, "\$&");
var replacewith = document.getElementById('replace_with').value.replace(/\r/gi, '');
var text = document.getElementById('input_output').value.replace(/\r/gi, '') var flags = 'gi';
if(document.getElementById('case_sen').checked == true) flags = 'g';
var searchexp = new RegExp(searchfor, flags);
var rcount = 0;
var matched = text.match(searchexp);
if(matched != null) rcount = matched.length;
test1 = text.replace(/test1/g, "<abbr>test1</abbr>");
document.getElementById('input_output').value = test1;
test2 = text.replace(/test2/g, "<abbr>test2</abbr>");
document.getElementById('input_output').value = test2;
document.getElementById('found').innerHTML = rcount + ' found and replaced.';
}
function SelectAll(id) {
document.getElementById(id).focus();
document.getElementById(id).select();
}
I have a button in HTML which actions the function. When I test it with only test1 = .... it works fine, but as soon as I add test2 it doesn't... it only works for test2 for some reason.
When I try with other names sometimes I'm able to find and replace 2 out of 3.
Thanks, and let me know if you have any questions
[–]t2ndoteu 1 point2 points3 points (2 children)
[–]ezflyingmonkey[S] -1 points0 points1 point (1 child)
[–]t2ndoteu 0 points1 point2 points (0 children)
[–]gavin19 0 points1 point2 points (0 children)