The answer to your IT questions
 

Advanced search  • Search tips
My QUESTIONS & ANSWERS
 Question:

How to find and replace multiple strings?

User Asked by: alexander
Published on: 00:37/14.01.2009
Status: OPEN
How to find multiple strings in my html, for example:

<p>
    string Lorem Ipsum Lorem string Ipsum 
     <p>
        <span>Lorem Ipsum string Lorem Ipsum Lorem Ipsum</span>
     </p>
</p>


And how can I replace the words "string" ... like this:

  <span>s</span><span>t</span><span>r</span><span>i</span><span>n</span>  <span>g</span>


..or with other word?
 Answers: 1
Sort by: /\ date | rating
Image Answer by: xpert
Posted on: 23:37/13.01.2009
Rating: 4 from possible 5 with 4 votes
There is direct function in javascript "replace" which uses regular expression. Option "g" is included at the end of regular expression to indicate to replace all occurances. You can insert "i" option to make search case insensitive.
Here is complete HTML example:


<html>
<head>
<script>
function replaceStr(s) {

	return s.replace(/string/g, "<span>s</span><span>t</span><span>r</span><span>i</span><span>n</span>  <span>g</span>");
}
</script>
</head>
<body>
	<br>
	Original:<br>
	<TEXTAREA id="central" cols="70" rows="5">
	<p>    string Lorem Ipsum Lorem string Ipsum      <p>        <span>Lorem Ipsum string Lorem Ipsum Lorem Ipsum</span>     </p></p>
	</TEXTAREA>
	<br>
	Replaced:<br>
	<TEXTAREA id="replaced" cols="70" rows="5">

	</TEXTAREA><br>

	<button onclick="document.getElementById('replaced').value = replaceStr(document.getElementById('central').value)">Go</button>
</body>
</html>


You can read more here :

http://www.w3schools.com/jsref/jsref_replace.asp
Vote:

Please vote! Your opinion matters!
If you haven't found what you've looking for, post a question
  
| Home | Hall of fame | Register | Log in | Terms of service | Help | Contacts |