Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. Removing a simple pattern from a string!

Removing a simple pattern from a string!

Scheduled Pinned Locked Moved Web Development
helpjavascriptregexquestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mehdi Mousavi
    wrote on last edited by
    #1

    i folks, Consider the following JavaScript function: function removeParam(str, name) { var rgx = new RegExp('(' + name + '=\\w*)|(' + name + '=\\w*;)'); rgx.global = true; rgx.ignoreCase = true; var matches = rgx.exec(str); if(matches == null) return str; var i; for(i = 0; i < matches.length; i++) str = str.replace(matches[i], ''); return str; } and the following call: var cookie = 'vid=39; vid=38; ASPNETSESSION=WHATEVER; vid=39'; alert(removeParam(cookie, 'vid')); I actually would like to remove "vid=(whatever number)" from the above string, so that it results in the following string: ASPNETSESSION=WHATEVER would someone please helps me to fix the problem? I've got no idea what's wrong with the above pattern I've written. Thank you for your time. Mehdi Mousavi - Software Architect [ http://mehdi.biz ]

    G 1 Reply Last reply
    0
    • M Mehdi Mousavi

      i folks, Consider the following JavaScript function: function removeParam(str, name) { var rgx = new RegExp('(' + name + '=\\w*)|(' + name + '=\\w*;)'); rgx.global = true; rgx.ignoreCase = true; var matches = rgx.exec(str); if(matches == null) return str; var i; for(i = 0; i < matches.length; i++) str = str.replace(matches[i], ''); return str; } and the following call: var cookie = 'vid=39; vid=38; ASPNETSESSION=WHATEVER; vid=39'; alert(removeParam(cookie, 'vid')); I actually would like to remove "vid=(whatever number)" from the above string, so that it results in the following string: ASPNETSESSION=WHATEVER would someone please helps me to fix the problem? I've got no idea what's wrong with the above pattern I've written. Thank you for your time. Mehdi Mousavi - Software Architect [ http://mehdi.biz ]

      G Offline
      G Offline
      Guffa
      wrote on last edited by
      #2

      Perhaps you should mention what the problem is? It would greatly simplify fixing it... ;) I don't think that your replace is working the way you think. At least you should skip the first item in the array, as it contains the entire matched string, not a matched group. Why don't you just use the RegExp object in a replace? function removeParam(str, name) { var rgx = new RegExp('(^|; )' + name + '=\\w*(?:; )?'); rgx.global = true; rgx.ignoreCase = true; return str.replace(rgx, '$1'); } I simplified the expression a bit, and made it aware of the beginning of the string or the separator before the name, so that it doesn't mess up other values. Otherwise removing the "on" property from "on=42; ASPNETSESSION=WHATEVER" would result in "ASPNETSESSI". Reservation for typos, though. The code is not tested. --- b { font-weight: normal; }

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups