RegEx noob question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • neptune6jun44@gmail.com

    RegEx noob question

    I need to take a string and allow only the first occurrence of an
    apostrophe for the validation of a city name...

    Example: D'Iberville, MS

    Say someone enters D'I'berville, MS by mistake..

    I'm able to retain all the apostrophes with the following:

    city.replace(/[^a-zA-Z0-9\'\$\,\.\#\s]/g,"");

    I want to retain the first apostrophe only. I've tried adding {2,} in
    with the apostrophe, but no luck.

    An example of the proper way to do this would be greatly appreciated.

    Thanks.
  • Kiran Makam

    #2
    Re: RegEx noob question

    On Sep 19, 6:01 am, "neptune6ju...@ gmail.com"
    <neptune6ju...@ gmail.comwrote:
        city.replace(/[^a-zA-Z0-9\'\$\,\.\#\s]/g,"");
    In string replace method, second parameter can be a function. You can
    make use of this like:
    -----------
    var str = "D'I'ber'v'i'll e";
    var myRe = /([^']*')(.*)/;

    str = str.replace(myR e,
    function (matchStr, strInParanthesi s1, strInParanthesi s2) {
    return strInParanthesi s1 + strInParanthesi s2.replace(/'/g,
    "");
    }
    );
    -----------

    Output:
    "D'I'ber'v'i'll e" will become "D'Ibervill e"
    "'DIber'v'i'lle " will become "'DIbervill e"

    - Kiran Makam



    Comment

    • Dr J R Stockton

      #3
      Re: RegEx noob question

      On Sep 19, 2:01 am, "neptune6ju...@ gmail.com"
      <neptune6ju...@ gmail.comwrote:
      I want to retain the first apostrophe only.
      S = "1'2'3'4'5"
      S.replace("'", "\xFF").replace (/'/g, "").replace("\x FF", "'")

      There, \xFF can be replaced by any chatacter that cannot occur in the
      input.

      S = "1'2'3'4'5"
      P = S.indexOf("'")
      S = S.substring(0,P +1) + S.substring(P+1 ).replace(/'/g, "")

      --
      (c) John Stockton, near London, UK. Posting with Google.
      Mail: J.R.""""""""@ph ysics.org or (better) via Home Page at
      Web: <URL:http://www.merlyn.demo n.co.uk/>
      FAQish topics, acronyms, links, etc.; Date, Delphi, JavaScript, ...

      Comment

      • Bart Van der Donck

        #4
        Re: RegEx noob question

        Dr J R Stockton wrote:
        <neptune6ju...@ gmail.comwrote:
        >I want to retain the first apostrophe only.
        >
        S = "1'2'3'4'5"
        S.replace("'", "\xFF").replace (/'/g, "").replace("\x FF", "'")
        I think this would be the most efficient, yes. If there were a single
        regex, it would go like this:

        /(?<=')([^']*)'/g,'$1'

        or

        /(?<=')(.*?)'/g,'$1'

        But javascript only supports look-ahead (?=/?!) and not look-behind (?
        <=/?<!); so there is no other choice than finding a workaround. It
        already struck me a few times that javascript has some gaps in the
        language: no regex look-behind, no reverse()-property for strings, ...

        --
        Bart

        Comment

        Working...