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 5942609
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:16:40+00:00 2026-05-22T16:16:40+00:00

Can anyone help me with the following regex <script type=text/javascript> function quoteWords() { var

  • 0

Can anyone help me with the following regex

<script type="text/javascript">
        function quoteWords() {
            var search = document.getElementById("search_box");
            search.value = search.value.replace(/^\s*|\s*$/g, ""); //trim string of ending and beginning whitespace
            if(search.value.indexOf(" ") != -1){ //if more then one word
                search.value = search.value.replace(/^"*|"*$/g, "\"");
            }
        }
  </script>

<input type="text" name="keywords" value="" id="search_box" size="17">
<input onClick="quoteWords()" type="submit" value="Go">

Issue : It breaks when manually adding double quotes and pressing submit, one extra double quote is entered at the end. The regex code should see if the double quotes exist, it should not add any thing.

So it makes "long enough" to "long enough"" <- it adds an extra double quote at the end

Can anyone check the regex code so see how to solve this issue.

I only want the double quotes to be inserted once.

  • 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-05-22T16:16:41+00:00Added an answer on May 22, 2026 at 4:16 pm

    The error is definitely happening in this line:

    search.value = search.value.replace(/^"*|"*$/g, "\"");
    

    And it is due to the fact that “* matches 0 or more quotes. However, you presumably wouldn’t want to just replace it with “+ since that wouldn’t do the job you wanted of double-quoting strings with spaces in them.

    You probably just want to do something like this, in two statements:

    search.value = search.value.replace(/^"*|"*$/g, '')
    search.value = '"' + search.value + '"'
    

    Part of the key is that there is no ‘end of string’ character to consume – the regex engine ‘just knows’ when it is at the end of the string. So after matching a quote at the end of the string, the cursor just moves to the end of the string, and it finds the empty string one more time before falling off the string. Thus, the quote at the end of the string is replaced by a quote, and the ‘nothing’ at the end of the string is also replaced by a quote.

    I recommend taking a look at the ECMAScript spec at http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-262.pdf sections 15.5.4.10 and 15.5.4.11 yourself. However, I’ve also provided an intuitive illustration of how this works at this gist.

    EDIT:

    Since people seem confused as to why this would happen, here’s something that might help:

    http://www.grymoire.com/Unix/Sed.html#uh-6

    That’s from the documentation for sed, but it explains why combining * and /g is a bad idea. The fact that JS doesn’t just explode when you do that is a mark in its favor. Note that there are an infinite number of ‘0 characters’ at every position in the string.

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

Sidebar

Related Questions

Can anyone help me with a Javascript question, please? Why does the following code
am having problems with some Regex code can anyone help. I have the following
I have the following text as an example: \n\t\t\t\t\t3 comments Can anyone help me
Can anyone help? I have the following structure using associations, and as you can
I am new in Blackberry. Can anyone help me out for following queries? Which
Can anyone please help me to work out how to achieve the following? I
I am new in Symbian UIQ. So anyone can help me out about following
Can anyone help? I have been designing a site using Javascript but the rest
can anyone help me in trying to check whether JavaScript is enabled in client
Can anyone help me explain how TimeProvider.Current can become null in the following class?

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.