javascript replace...'<::' with '<//'

I need to replace the text with the above..it appears that javascript uses regex??
LVL 10
GlobaLevelProgrammerAsked:
Who is Participating?
 
hieloCommented:
>>Hielo...good stuff but the string the user supplies is very long and I have to find these sequences...will that work?
Yes. Read comments in code examples below:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
<title>Untitled</title>
<script type="text/javascript">
function transferData(from,to)
{
	document.getElementById(to).value=document.getElementById(from).value.split('<::').join('<//')
}
</script>
</head>
<body>
<form method="post" action="yourpage.php">
	<div><input type="button" onclick="transferData('from','to')" value="Clean Data"/></div>
	<div><textarea id="from" name="notes">Hello <:: blah <:: blah </textarea></div>
	<div><textarea id="to"   name="copyOfNotes"></textarea></div>
</form>
</body>
</html>

Without the button (as soon as you put content in the first one and then "leave"/"click out", the function will be called): 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
<title>Untitled</title>
<script type="text/javascript">
function transferData(from,to)
{
	document.getElementById(to).value=document.getElementById(from).value.split('<::').join('<//')
}
</script>
</head>
<body>
<form method="post" action="yourpage.php">
	<div><textarea id="source"        name="notes" onchange="transferData('source','destination')"></textarea></div>
	<div><textarea id="destination"   name="copyOfNotes"></textarea></div>
</form>
</body>
</html>

Open in new window

0
 
GlobaLevelProgrammerAuthor Commented:
any?
0
 
AmickCommented:
<html>
<body>

<script type="text/javascript">

var str="This is &lt::! ";
str=str + "I want to change &lt:: to &lt// ";

document.write(str,'<br />');
document.write("like this","<br/>",str.replace(/&lt::/gi, "&lt//"));

</script>

</body>
</html>
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
hieloCommented:
split the string at the <::

then immediately joint it with <//

No regex needed:
<script type="text/javascript">

var str="This is <::! blah blah  <::! blah ";
str=str + "I want to change <:: to <// ";

alert("Input:\n"  + str);
alert("Output:\n" + str.split('<::').join('<//') );

</script>

Open in new window

0
 
GlobaLevelProgrammerAuthor Commented:
Ooo...one more I have a textarea where the user enters info...they click on a HTML button...is there a way to connect the data in your code to what the user enters ?
0
 
GlobaLevelProgrammerAuthor Commented:
And out put to a second textarea..

Hielo...good stuff but the string the user supplies is very long and I have to find these sequences...will that work?
0
 
AmickCommented:
The alternate code for using replace would be to substitute this at line 31:

document.getElementById(to).value=document.getElementById(from).value.replace(/&lt::/gi, "&lt//"));

 
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.