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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

RegExp - this may be simple... 2

Status
Not open for further replies.

theocraticmind

Programmer
Apr 19, 2002
318
CA
ok, i realy don't know how this RegExp thing works, but i've read i need to use it in the replace() function. so, this is my code, what am i doing wrong?
Code:
var strTemp

regExpBlack = new RegExp("[bg=black]", "i")
regExpRed = new RegExp("[bg=red]", "i")
regExpBlue = new RegExp("[bg=blue]", "i")
regExpGreen = new RegExp("[bg=green]", "i")
regExpOrange = new RegExp("[bg=orange]", "i")
regExpYellow = new RegExp("[bg=yellow]", "i")

strNothing = new String("")

strTemp = document.frmChange.txtContent.value
strTemp.replace(regExpBlack, strNothing)
strTemp.replace(regExpRed, strNothing)
strTemp.replace(regExpBlue, strNothing)
strTemp.replace(regExpGreen, strNothing)
strTemp.replace(regExpOrange, strNothing)
strTemp.replace(regExpYellow, strNothing)
document.frmChange.txtContent.value = strTemp
 
oh, and incase you are wondering, there are no errors. it just dosen't do what is should.
 
to replace the substring "[bg=black]" (or whatever other color i specify) with "".
 
part of the problem is that you need to assign the value returned by the string.replace method. calling string.replace does not modify the string calling the method. it simply returns the value of the modified string.

the other part of the problem are the "[" and "]" characters. they have special meaning with regular expressions. for example, "[black]" finds any single occurance of b, l, a, c, or k. it does not find "[black]". you need to use the "\" before any special character so that the character doesn't impart some special meaning. things get a little bit more complicated because the backslash also has special meaning in strings, which is what you use with the RegExp contructor.

so, the short of it is as follows...

Code:
var stripExp = new RegExp("\\[bg=black\\]","i");

...or...

Code:
var stripExp = /\[bg=black\]/i;

...followed later by...

Code:
strTemp = strTemp.replace(stripExp,"");

that should work for you.

glenn
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top