Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
需要javascript正则表达式帮助_Javascript_Jquery_Regex - Fatal编程技术网

需要javascript正则表达式帮助

需要javascript正则表达式帮助,javascript,jquery,regex,Javascript,Jquery,Regex,昨天我在这个话题上寻求了帮助,但我需要扩展它并在正则表达式中添加一些内容。以下是我到目前为止为去除字符串中不需要的单词所做的工作,但现在我还需要替换非单词字符,如句点、逗号、斜杠、星号等。如果这是我需要使用的,我如何添加\W?我尝试了许多变体,但都没有成功。请帮忙 <input id="search" type="text" value="bone on ^ * () a thing there and an hifi if the offer, of boffin."/>

昨天我在这个话题上寻求了帮助,但我需要扩展它并在正则表达式中添加一些内容。以下是我到目前为止为去除字符串中不需要的单词所做的工作,但现在我还需要替换非单词字符,如句点、逗号、斜杠、星号等。如果这是我需要使用的,我如何添加\W?我尝试了许多变体,但都没有成功。请帮忙

<input id="search" type="text" value="bone on ^ * () 
  a thing there and an hifi if the offer, of boffin."/>

$("#search").bind('enterKey', function(e){
var search = $('#search')
               .val()
               .replace( /\b(on|a|the|of|in|if|an)\b/ig, '' )
               .replace( /\s+/g, '-' );
alert( 'Replace spaces: ' + search);
});

$('#search').keyup(function(e){
if(e.keyCode == 13){
  $(this).trigger("enterKey");
}
});

$(“#搜索”).bind('enterKey',函数(e){
var search=$(“#search”)
.val()
.替换(/\b(在| if | an中|的| a |上)b/ig“”)
.替换(/\s+/g,“-”);
警报(“替换空格:”+搜索);
});
$(“#搜索”).keyup(函数(e){
如果(e.keyCode==13){
$(此).trigger(“enterKey”);
}
});

如果您只想删除它们,这将实现以下功能:

$("#search").bind('enterKey', function(e){
    var search = $('#search')
                   .val()
                   .replace(/\b(on|a|the|of|in|if|an)\b|\W/ig, '')
                   .replace(/\s+/g, '-');
    alert( 'Replace spaces: ' + search);
});
$('#search').keyup(function(e){
if(e.keyCode == 13)
{
  $(this).trigger("enterKey");
}
});
如果要用其他字符替换所有非单词字符,请使用以下命令:

$("#search").bind('enterKey', function(e){
    var search = $('#search')
                   .val()
                   .replace(/\b(on|a|the|of|in|if|an)\b/ig, '')
                   .replace(/[^a-zA-Z0-9_ ]/g, '*')
                   .replace(/\s+/g, '-');
    alert( 'Replace spaces: ' + search);
});
$('#search').keyup(function(e){
if(e.keyCode == 13)
{
  $(this).trigger("enterKey");
}
});

添加更容易添加:
\.-\[\]\(\)..
no?或者您可以筛选
/[^A-Za-z0-9]/
,看起来您可能试图从用户输入生成URL友好的slug。如果是这样,也许你可以看看?谢谢…第二个选择正是我想要的。我刚刚修改了第二个replace语句,将“*”改为“”,所以我没有得到一大堆破折号,只有一条。再次感谢!