• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Regular Expression with variable in Javascript

 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I am new to RegEx and have a requirement as follows
The String is of the following type




Here, I need to find ONE RegEx that matches all the above strings. Next, I have to replace the VARIABLE string inside the quotes i.e. World or Universe with the string of my choice
Please suggest how to write a RegEx to identify above string pattern and to replace the variable which is inside the quotes.

Thanks,
Faraz
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Start out small. Can you match any of the following? Where do you get stuck?

  • A string that begins with Hello
  • A string that begins with Hello and is followed by a space
  • A string that begins with Hello and is followed by a space and a quote
  • A string that begins with Hello and is followed by a space, a quote and zero or more spaces
  • A string that begins with Hello and is followed by a space, a quote and zero or more spaces and one more word
  • A string that matches your entire target expression


  • Then put the part of the regular expression that represents the part before and after the word in parenthesis. This creates two capturing groups so you can do the replacement.
     
    Ranch Hand
    Posts: 310
    18
    MS IE Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    As Jeanne asked, where do you get stuck?

    Using this program can be very helpful for you in learning regex.
     
    Did Steve tell you that? Fuh - Steve. Just look at this tiny ad:
    a bit of art, as a gift, the permaculture playing cards
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic