Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7895539
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T07:40:05+00:00 2026-06-03T07:40:05+00:00

I need help to build a regular expression. I’m programming a software that looks

  • 0

I need help to build a regular expression. I’m programming a software that looks for certain words in a string. But I need to also look for phrases. This string is entered by the user in a text box.

Currently I use the following regular expression \s+ to replace spaces by ampersands:
word1 word2 word3 to word1&word2&word3

My new requirement must include phrases enclosed in quotes, like the searching for phrases in Google.

"word1 word2" "word3 word4" word5

Must be:

word1 word2&word3 word4&word5

Thanks in advance.

EDIT: If there is another way or another approach to do the same, I’m open to any idea.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T07:40:06+00:00Added an answer on June 3, 2026 at 7:40 am

    This assumes the quests are well balanced – you have an even number of quests, and not quests in middle of words.

    You can match for /"([^"]+)"|(\S+)/ – this will result in your words – quoted strings or non-spaces. It captured the word in group 1 or 2, and you can then join the results with the & delimiter.

    Another option: you can get it in a single replace, by which is very similar way to the first pattern, by skipping over your tokens. Here’s a JavaScript example:

    s = s.replace(/(?:"([^"]+)"|(\S+))\s*/g, '$1$2&');
    

    (note that you will have an extra ampersand in the end of the string)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

need help to create regular expression matching string www.*.abc.*/somestring Here * is wild card
I need help to built regular expression for string which does not start with
I need help to build a regex that can remove EVEN lines in a
Please help to the newbie in WPF! I need to build a TreeView with
I need help on regex or preg_match because I am not that experienced yet
I need help finishing this statement. It is frustrating that two of the PHP
I need help in resolving a complex SQL query. I am trying to build
What are the option in terms of applications that help build SQL statements against
I need some help to build SQL Query. I have table having data like:
I need some help to build SQL Query. I have table having data like:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.