Regular Expression replace location.href

B

bus105

I am trying to create a function to strip any occurrence of the string
value of the current location.href from another string.

Here is the basic idea:

var str; //the long string
var reStr; //the new string

x = '/' + location.href + '/g'
re = eval(x);
reStr = str.replace(re, '');
 
B

bus105

I created a work around, but still think a reg exp would be a better solution.

x = location.href
xlen = x.length
while(str.indexOf(x) != -1 ){
slen = str.length
a = str.indexOf(x)
b = str.indexOf(x)+ xlen
astr = str.substring(0,a)
bstr = str.substring(b,slen)
str = astr + bstr
}
reStr = str
 
E

Evertjan.

bus105 wrote on 02 dec 2003 in comp.lang.javascript:
var str; //the long string
var reStr; //the new string

x = '/' + location.href + '/g'
re = eval(x);
reStr = str.replace(re, '');

1 never use eval, it is evil.

2 the location.href probably has "/"-ses in it

Do:

var re = new RegExp(location.href,"g");
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top