Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    May 2004
    Posts
    392
    Thanks
    77
    Thanked 0 Times in 0 Posts

    Remove characters before and including.....

    Hi ALL!

    If I have a variable and need to remove all characters before and including a "/" or "-" how can I do that?

  2. #2
    Master Coder sunfighter's Avatar
    Join Date
    Jan 2011
    Location
    Washington
    Posts
    6,096
    Thanks
    30
    Thanked 837 Times in 835 Posts
    What language are you using. This is for DOM and JSON scripting.
    I would think setting the variable equal to '' (an empty string) would do the job.
    Would help if you gave us the string.
    Evolution - The non-random survival of random variants.
    Physics is actually atoms trying to understand themselves.

  3. #3
    Regular Coder
    Join Date
    May 2004
    Posts
    392
    Thanks
    77
    Thanked 0 Times in 0 Posts
    Just plain old javascript

  4. #4
    Master Coder sunfighter's Avatar
    Join Date
    Jan 2011
    Location
    Washington
    Posts
    6,096
    Thanks
    30
    Thanked 837 Times in 835 Posts
    Still didn't tell us the chars needed to be removed or post the string, but this will only leave a string (str) with alphanumeric charaters:
    str = str.replace(/[^a-zA-Z 0-9]+/g,'');
    Evolution - The non-random survival of random variants.
    Physics is actually atoms trying to understand themselves.

  5. Users who have thanked sunfighter for this post:

    nikko50 (05-01-2014)

  6. #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,889
    Thanks
    211
    Thanked 2,622 Times in 2,600 Posts
    Quote Originally Posted by nikko50 View Post
    Hi ALL!

    If I have a variable and need to remove all characters before and including a "/" or "-" how can I do that?
    Code:
    var txt = "abc/123?xyz";
    txt = txt.replace(/(.*\/)|(.*-)/i,"");
    alert (txt);  // 123?xyz
    If you require a Javascript solution it would be best to post in the Javascript forum.
    Last edited by Philip M; 05-01-2014 at 06:56 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


 

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •