SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Addict
    Join Date
    May 2007
    Posts
    242
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Regular Expression Help

    Hello. I have a string to change some part of it.

    Code JavaScript:
    "Today is 'so boring + morning' + heidi"

    I want to convert all plus (+) signs to minus signs (-) but not the ones inside single quotes. Result should be like below.

    Code JavaScript:
    "Today is 'so boring + morning' - heidi"

    I have tried to create the regular expression for replace function but couldnt managed to. Could you help me to create reguar expression, please?

    Thansk in advance.

  2. #2
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    s = "Today is 'so boring + morning' + heidi"
    q = s.replace(/'.*?'|\+/g, function($0) {
       return $0 == '+' ? '-' : $0
    })
    alert(q)

  3. #3
    SitePoint Addict
    Join Date
    May 2007
    Posts
    242
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you so much @stereofrog. It helped.


Tags for this Thread

Bookmarks

Posting Permissions

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