Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-12-2013, 02:40 PM   PM User | #1
RickGervs
New to the CF scene

 
Join Date: Feb 2013
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
RickGervs is an unknown quantity at this point
Need help with find and replace coding

Hi,

So I'm kind of new to JavaScript and I need to create a script that will grab the code I paste in a HTML page and do a few find and replace

When I use this code
Code:
test1 = text.replace(/test1/, "<abbr>test_new1</abbr>");
document.getElementById('input_output').value = test1;
it works for but as soon as I add another line like this

test1 = text.replace(/test1/, "<abbr>test_new1</abbr>");
document.getElementById('input_output').value = test1;

test2 = text.replace(/test2/, "<abbr>test_new2</abbr>");
document.getElementById('input_output').value = test2;


it doesn't work for some reason.

It's very unfortunate because I need to do this like 30-40 times.

I included my file so it'll be easier for you.

This is just a small application to help my team internally it's nothing that'll go online

Thanks!!!!!!!
Attached Files
File Type: zip test.zip (1.3 KB, 14 views)
RickGervs is offline   Reply With Quote
Old 02-12-2013, 03:01 PM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,036
Thanks: 197
Thanked 2,411 Times in 2,389 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Code:
<div id = "input_output"> Here is test1 and also test2</div>

<script type = "text/javascript">
var text = document.getElementById("input_output").innerHTML;
var reptext =  text.replace(/\btest1\b/, "<abbr>test_new1</abbr>");
reptext = reptext.replace(/\btest2\b/, "<abbr>test_new2</abbr>");
alert (reptext);  
document.getElementById('input_output').innerHTML = reptext;
</script
If the text is in a textarea then you want to replace .innerHTML with .value

Note the \b tags indicating a word boundary (whole words only).

Quizmaster: Who is married to the Duke of Edinburgh?
Contestant: Kate Winslet.
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Philip M is online now   Reply With Quote
Old 02-12-2013, 05:42 PM   PM User | #3
RickGervs
New to the CF scene

 
Join Date: Feb 2013
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
RickGervs is an unknown quantity at this point
Wow this is exactly what I needed thank you very much!!
RickGervs is offline   Reply With Quote
Old 02-12-2013, 05:49 PM   PM User | #4
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,036
Thanks: 197
Thanked 2,411 Times in 2,389 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Glad to hear it! You may wish to add g and i flags to make the replace global (not just the first instance) and case-insensitive.

Code:
<script type = "text/javascript">
var text = document.getElementById("input_output").innerHTML;
var reptext =  text.replace(/\btest1\b/gi, "<abbr>test_new1</abbr>");
reptext = reptext.replace(/\btest2\b/gi, "<abbr>test_new2</abbr>");
alert (reptext);  
document.getElementById('input_output').innerHTML = reptext;
</script>
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Philip M is online now   Reply With Quote
Old 02-12-2013, 06:02 PM   PM User | #5
RickGervs
New to the CF scene

 
Join Date: Feb 2013
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
RickGervs is an unknown quantity at this point
That's exactly what I did, thanks again, much appreciated
RickGervs is offline   Reply With Quote
Reply

Bookmarks

Tags
find, javascript, noob, replace, test

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:59 AM.


Advertisement
Log in to turn off these ads.