最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

regex - How to automatically add Double Quotes to enclose a string via Javascript? - Stack Overflow

programmeradmin1浏览0评论

I would like to enclose a search term with Double quotes automatically

For e.g.

<input type="text" name="keywords" value="" id="search" size="17" />
<input type="submit" value="Go">

a visitor tries to search for a phrase = long enough (it contains 2 words with a space in between)

I need the string long enough to be passed as "long enough" to server side (PHP)

I dont want to tell the visitor to use Double quotes for searching for a phrase, for the visitor the search should be easy.

Presently, when a visitor enters a search term with one word long the search results show correct articles.

But when a visitor uses a phrase long enough or any other phrase, using a minimum of 2 words, the search results are not accurate as the search take long enough as 2 seperate search terms.

For accurate search results, the ExpressionEngine CMS wants the visitor to enter "long enough" [any phrase to be enclosed in double quotes]

This is what i want to do automatically, when a visitor searches for one word, it should leave the keyword and let it pass without any quotes, but when a visitor uses 2 or more words, the search form should pass those phrases in Double quotes


Code provided by @Jonathon Wisnoski works as i want it, but with one drawback

 <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>

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""

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

I would like to enclose a search term with Double quotes automatically

For e.g.

<input type="text" name="keywords" value="" id="search" size="17" />
<input type="submit" value="Go">

a visitor tries to search for a phrase = long enough (it contains 2 words with a space in between)

I need the string long enough to be passed as "long enough" to server side (PHP)

I dont want to tell the visitor to use Double quotes for searching for a phrase, for the visitor the search should be easy.

Presently, when a visitor enters a search term with one word long the search results show correct articles.

But when a visitor uses a phrase long enough or any other phrase, using a minimum of 2 words, the search results are not accurate as the search take long enough as 2 seperate search terms.

For accurate search results, the ExpressionEngine CMS wants the visitor to enter "long enough" [any phrase to be enclosed in double quotes]

This is what i want to do automatically, when a visitor searches for one word, it should leave the keyword and let it pass without any quotes, but when a visitor uses 2 or more words, the search form should pass those phrases in Double quotes


Code provided by @Jonathon Wisnoski works as i want it, but with one drawback

 <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>

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""

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

Share Improve this question edited May 27, 2011 at 2:50 Ibn Saeed asked May 27, 2011 at 1:21 Ibn SaeedIbn Saeed 3,30112 gold badges51 silver badges60 bronze badges 4
  • 2 The term long enough should appear on the server as a single string. If you really want to wrap that single string in double quotes, so that the quotes are part of the string, just add them to the front and back. Unless I am mis-understanding your question? – Jeremy Commented May 27, 2011 at 1:24
  • 1 Why can't your PHP script do this? Anything done on the client can be circumvented. Also, you're not doing this in order to interpolate it into a string of SQL, right? – Cameron Commented May 27, 2011 at 1:25
  • @Jeremy Heiler, i have just added more information to the original question. – Ibn Saeed Commented May 27, 2011 at 1:40
  • Why not do this on the server? Do you have access to the server code to make changes? – Tobias Cohen Commented May 27, 2011 at 1:50
Add a ment  | 

3 Answers 3

Reset to default 3

You should really just handle the string as is server side. Anything preprocessed with JavaScript can't be relied on.

However, if you did want to do this, there is a non standard String method called quote().

However, because it is non standard, I would use...

str = '"' + str.replace(/^"*|"*$/, '') + '"';

This strips leading and trailing " and then wraps the string again in ".

Since you're trying to validate the input to some extent, it might make more sense to do something a little more general - for example, if the user enters "foo "bar", you presumably want it to end up looking like "foo bar". So instead of:

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

You might want to do something like:

str = '"' + str.replace(/"/g, '') + '"'

And that's a lot simpler to look at later.

This should do it:

<input type="text" name="keywords" value="" id="search" size="17" />
<input onClick='var search = document.getElementById("search"); search.value = search.value.replace(/^\s*|\s*$/g, ""); if(search.value.indexOf(" ") != -1){search.value = search.value.replace(/^"*|"*$/g, "\"")}' type="submit" value="Go">

Or with the script in a script tag:

<script type="text/javascript">
    function quoteWords() {
        var search = document.getElementById("search");
        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" size="17">
<input onClick="quoteWords()" type="submit" value="Go">
发布评论

评论列表(0)

  1. 暂无评论