RegExp help!!

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

    RegExp help!!

    Hi, all!! Need a hand with a regular expression... Let's say I have this
    string:
    "Now is 'the time' for all 'good men' to come to 'the aid' of their country"

    I want to delete each occurrance of any string contained within the
    apostrophes, including the apostrophes. So in the above example, the result
    should be:
    "Now is for all to come to of their country"

    Tried several different expressions, but can't find one that works... Any
    ideas?

    Thanks!
    Jack


  • Sergey I.Grachyov

    #2
    Re: RegExp help!!

    Hi!

    Use this script, please:

    var s = "Now is 'the time' for all 'good men' to come to 'the aid' of their
    country"

    s = s.replace(/\'.+?\'/g,'');

    alert(s);


    --
    Sergey.
    This url return “online”:false in any case: https://www.freelancer.com/ajax/user/isOnline.php?user_id=10663 As for example: {“status”:”success”,”result”:{“online”:false,”last_login”:1494827557}} Please, use your own id to check your status. My status is “online”:false in any case (any computer, any web browser). Support ticket #ROT-837-43885 sent 10 days ago. Result: ” We’ll forward your concern to the Engineering team and we’ll contact you again […]



    "Jack Black" <jackblackisbac k@hotmail.com> wrote in message
    news:3f96ea89_3 @127.0.0.1...[color=blue]
    > Hi, all!! Need a hand with a regular expression... Let's say I have this
    > string:
    > "Now is 'the time' for all 'good men' to come to 'the aid' of their[/color]
    country"[color=blue]
    >
    > I want to delete each occurrance of any string contained within the
    > apostrophes, including the apostrophes. So in the above example, the[/color]
    result[color=blue]
    > should be:
    > "Now is for all to come to of their country"
    >
    > Tried several different expressions, but can't find one that works... Any
    > ideas?
    >
    > Thanks!
    > Jack
    >
    >[/color]


    Comment

    • Thomas 'PointedEars' Lahn

      #3
      Re: RegExp help!!

      Sergey I.Grachyov wrote:
      [color=blue]
      > var s = "Now is 'the time' for all 'good men' to come to 'the aid' of their
      > country"
      >
      > s = s.replace(/\'.+?\'/g,'');
      >
      > alert(s);[/color]

      Unfortunately, this will not work prior to JavaScript 1.5 where non-greedy
      modifiers were implemented at first. Therefore it will probably not work in
      Internet Explorer prior to version 6.0 SP-1, Opera below version 7.0 (CMIIW)
      and definitely not in Netscape prior to 6.0 (Mozilla prior to /5.0).

      However, if you cannot make a RegExp non-greedy, you can still design it
      not to match substrings containing specific characters, so

      s = s.replace(/'[^']+'/g, '');

      will be greedy but will only match what is between each pair of "'",
      and not "'the time'...'the aid'". And it will most certainly be
      compatible with other UAs than Mozilla/5.0.

      BTW: "'" within RegExp literals need not to be escaped.


      PointedEars

      Comment

      Working...