Greg Ostry
Greg Ostry

Reputation: 1231

How to detect whitespace in a string in javascript

how do i detect a whitespace "space" in a string. My string are somtehing like this 0651160403XL 00CBD012

my code:

<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>

but my code is not working. How can i escape the slash in match function?

Upvotes: 0

Views: 102

Answers (2)

Shigehiro Kamisama
Shigehiro Kamisama

Reputation: 183

Use :

input.match(/\s+/g) 

Or :

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

Upvotes: 0

Piyush Kumar Baliyan
Piyush Kumar Baliyan

Reputation: 404

You can simply use .replace(/\s+/g,'') `

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

`

Upvotes: 1

Related Questions