在搜索表单中,替换空格或仅使用正则表达式的隐词和数字


Replace space or use regex only aphabets and numbers on search form

我有这个表单

<form action="search.php" method="get">
Keyword: <input type="text" name="keyword"><br>
<input type="submit">
</form>

当搜索ex."FirstName LastName"时,url将转到

example.com/search.php?keyword=FirstName LastName

但是我想要这个网址

example.com/search.php?keyword=firstname-lastname

我需要用javascript将"(空格)替换为"-"吗?如果需要,如何替换?

谢谢。

<input id="text1" type="text" />
<input id="text2" type="text" />
<button id="test">Test</button>
<script>
$.fn.copyTo = function(selector) {
    $(selector).val($(this[0]).val().replace(/'s/g, "-"));
};
$(document).ready(function () {
    $("#test").click(function() {
        $("#text1").copyTo("#text2");
    });
});
</script>

来源:如何在将值从表单字段复制到另一个时用下划线替换空白

示例:http://jsfiddle.net/cUTXs/

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