如何在javascript中检测字符串中的空格

时间:2017-05-15 12:51:47

标签: javascript regex

我如何检测空白"空间"在一个字符串中。 我的字符串像这样0651160403XL 00CBD012

我的代码:

<input type="text" name="myLabel"><br>
<span></span>
<script>
$("input[name='myLabel']").on('keyup', renderInput);
function renderInput() {
input = $(this).val();
if(input.match("/\s+/g"))
//trim the input to 0651160403XL
});
</script>

但我的代码无效。如何在匹配函数中转义斜杠?

2 个答案:

答案 0 :(得分:1)

您只需使用.replace(/\s+/g,'') `

$("input[name='myLabel']").on('keyup', renderInput);
function renderInput() {
  input = $(this).val();
  $(this).val(input.replace(/\s+/g,''));
}

`

答案 1 :(得分:0)

使用:

input.match(/\s+/g) 

或者:

Var reg = new regex("\\s", "g")