繁体   English   中英

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

[英]How to detect whitespace in a string in javascript

如何检测字符串中的空格“空格”。 我的绳子像这样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>

但我的代码无法正常工作。 如何在比赛功能中逃脱斜线?

您可以简单地使用.replace(/\\s+/g,'') `

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

`

采用 :

input.match(/\s+/g) 

要么 :

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM