Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

a strange problem with RegExp

Status
Not open for further replies.

akwah

MIS
Sep 26, 2010
2
SA
Hi,
I am trying to replace smilies in the code with there respective images but I am facing a strange problem .... take a look at my code

Code:
<html>
<head>
</head>

<body>

this is body text ha ha ha :) :)

<input type="submit" name="myButton" id="myButton" />
<script type="text/javascript">

        var myButton = document.getElementById("myButton");
        myButton.onclick = function() { 
			var myBody = document.body.innerHTML;
			//var re = new RegExp(":)","gi"); // line 1
			var re = new RegExp("o","gi"); // line 2
			myBody = myBody.replace(re, "[IMG]");
			document.body.innerHTML = myBody;
				
		
		};


</script>
</body>
</html>

as u can see the code will work perfectly like this but if I comment line 2 and uncomment line 1 it will not work ... and I can't know why .... I hope someone can help
 
>var re = new RegExp(":)","gi"); // line 1
Either this:
[tt]var re = new RegExp(":[red]\\[/red])","g"); // line 1[/tt]
or this:
[tt]var re = /:[red]\[/red])/g; // line 1[/tt]

ps: i flag is irrelevant here, so I take it out.
 
thanks tsuji ... I was under the impression that of construct a RegExp object you can use any text inside with no escape characters .... how do I know which characters need to be escaped ?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top