0

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?

4
  • 2
    Possible duplicate of Check if a string has white space Commented May 15, 2017 at 12:53
  • May you add example inputs with their expected outputs? Commented May 15, 2017 at 12:53
  • 2
    Use regex without quotes: input.match(/\s+/g) Commented May 15, 2017 at 12:54
  • Maybe something like if (/\s/.test(input.trim())) { $(this).val(input.trim().split(/\s/)[0]); }. The question is somewhat unclear. Commented May 15, 2017 at 13:00

2 Answers 2

1

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

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

`

Sign up to request clarification or add additional context in comments.

Comments

0

Use :

input.match(/\s+/g) 

Or :

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.