Welcome to our forums...

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed.

Forum Statistics

  • Forum Members:
  • Total Threads:
  • Total Posts: 4
There are 1 users currently browsing forums.
JavaScript Coding JavaScript allows rich, browser-based web-applications with interactivity and complex visual effects. For discussion on JavaScript, use this forum.

Reply
  #1  
Old 03-17-2010
Computer Zombie
 
Join Date: Apr 2009
Posts: 7
Rep Power: 0
deadIMPULSE is on a distinguished road
Need some help...

I am working on a text replacement page, and I need a bit of help. I want to condense text with javascript. I am using something like this:

var str="at to too"
document.write(str.replace(/at/i,"@"));

This works fine, but I want to replace the to and too in the text also. Any ideas how I could do this?
Reply With Quote
  #2  
Old 03-19-2010
Mancunian
 
Join Date: Mar 2010
Location: Greater Manchester, UK
Age: 18
Posts: 23
Rep Power: 0
Dumaju is on a distinguished road
Re: Need some help...

Code:
var str = "at to too";
var str = str.replace(/at/i, "@");
document.write( str.replace(/t(o){1,2}/g, "place replacetext here"));
This seemed to do the trick.

Basically, the second replace function is matching only to and too. This matches a single 't' literal and either one or two 'o' literals.
Reply With Quote
  #3  
Old 03-19-2010
Computer Zombie
 
Join Date: Apr 2009
Posts: 7
Rep Power: 0
deadIMPULSE is on a distinguished road
Re: Need some help...

Ahh, hadn't thought of that. Makes total sense. Thanks for that!
Alright, one more problem...

Here is the code:
Code:
<html>
<head>
<script type="text/javascript">

function condense() {
var str=document.input.inputfield.value;
str = str.replace(/at/i,"@")
str = str.replace(/to/i,"2")
str = str.replace(/too/i,"2")
str = str.replace(/tommorow/i,"2morrow")
str = str.replace(/you/i,"u")
str = str.replace(/ok/i,"k")

document.output.outputfield.value=(str);
}
</script>
</head>
<body>


<form name="input">
<label>Input:</label>
<br />
<textarea rows="10" cols="30" name="inputfield"></textarea>
<br />
<button onClick=condense();>Condense!</button>
</form>
<form name="output">
<label>Output:</label>
<br />
<textarea rows="10" cols="30" name="outputfield"></textarea>
</form>


</body>
</html>
It quickly dispays what I want in the output field, and then refreshes and disappears... Any ideas?

Thanks,
deadImpulse
Reply With Quote
  #4  
Old 03-20-2010
Toddler
 
Join Date: Mar 2010
Posts: 12
Rep Power: 0
ed7991 is on a distinguished road
Re: Need some help...

Quote:
deadIMPULSE originally posted: View Post
It quickly dispays what I want in the output field, and then refreshes and disappears... Any ideas?

Thanks,
deadImpulse
the problem is you've put it in forms
forms automaticly refresh the webpage
UNLESS you put the form action as "javascript:;"
so the ending code needs to be
Code:
<html>
<head>
<script type="text/javascript">

function condense() {
var str=document.input.inputfield.value;
str = str.replace(/at/i,"@")
str = str.replace(/to/i,"2")
str = str.replace(/too/i,"2")
str = str.replace(/tommorow/i,"2morrow")
str = str.replace(/you/i,"u")
str = str.replace(/ok/i,"k")

document.output.outputfield.value=(str);
}
</script>
</head>
<body>


<form name="input" action="javascript:;">
<label>Input:</label>
<br />
<textarea rows="10" cols="30" name="inputfield"></textarea>
<br />
<button onClick=condense();>Condense!</button>
</form>
<form name="output" action="javascript:;">
<label>Output:</label>
<br />
<textarea rows="10" cols="30" name="outputfield"></textarea>
</form>


</body>
</html>
Reply With Quote


Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes 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
Trackbacks are On
Pingbacks are On
Refbacks are On