By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,567 Members | 1,185 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,567 IT Pros & Developers. It's quick & easy.

Regular Expression replace location.href

P: n/a
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, '');
Jul 20 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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
Jul 20 '05 #2

P: n/a
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");

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jul 20 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.