Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 231
  • Last Modified:

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

I need to replace the text with the above..it appears that javascript uses regex??
0
GlobaLevel
Asked:
GlobaLevel
  • 3
  • 2
  • 2
1 Solution
 
GlobaLevelAuthor 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
 
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
GlobaLevelAuthor 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
 
GlobaLevelAuthor 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
 
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
 
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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now