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.
id="search_box"in the first INPUT element.