有人能帮我解决以下正则表达式吗?
<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">
问题:手动添加双引号并提交时会出现错误,会在末尾添加一个额外的双引号。正则表达式应该查看双引号是否存在,不应该添加任何内容。
所以它将"long enough"
变成了"long enough""
<- 在末尾添加了一个额外的双引号。
有人可以检查一下正则表达式代码,看如何解决这个问题。
我只希望双引号被插入一次。
id="search_box"
。 - Matty K