Regular Expression replace location.href

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • bus105

    Regular Expression replace location.href

    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, '');
  • bus105

    #2
    Re: Regular Expression replace location.href

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

    x = location.href
    xlen = x.length
    while(str.index Of(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

    Comment

    • Evertjan.

      #3
      Re: Regular Expression replace location.href

      bus105 wrote on 02 dec 2003 in comp.lang.javas cript:
      [color=blue]
      > var str; //the long string
      > var reStr; //the new string
      >
      > x = '/' + location.href + '/g'
      > re = eval(x);
      > reStr = str.replace(re, '');[/color]

      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)

      Comment

      Working...