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

non functioning [i]string[/i].replace() method

Status
Not open for further replies.

classefied

Programmer
Sep 15, 2005
5
US
I have a var(array) 'highlighted', which contains short sub-strings like 'd1;d4;d12;d110;'
and when I call the function using- remv(d1), the substring remains in the string - no matter what.
The [highlight #FF99FF]function remv(str){
var RegExp= "/", noHighlight = ""
noHighlight= '\;' ; RegExp+= '\b' + noHighlight + '\b\g';
highlighted.replace(RegExp,'')
}[/highlight]
I can continually add substrings to the main string, but cannot delete any.
Help me [highlight]please![/highlight]
 
1. "RegExp" is a reserved keyword in javascript - do not name a variable "RegExp" or you break the object RegExp()

2. if you want to remove the string "d1", you need to call remv("d1"), not remv(d1)

3. nowhere in your function remv() do you use the argument "str"!

try this:
Code:
var highlighted = 'd1;d4;d12;d110;';

function remv(str){
    var re = new RegExp("\\b" + str + ";", "gi");
    highlighted = highlighted.replace(re,'');
}

remv("d4");

alert(highlighted);

-jeff
try { succeed(); } catch(E) { tryAgain(); } finally { rtfm(); }
i like your sleeves...they're real big
 
Many thanks, I plugged in you suggestion and the function functioned for the first time in a week!!! I did not understand RegExp() was an object until now.
-->classefied
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top