2

I want to test this condition if the key event is backspace or not:

  if (key.match(/[\b]/)) {
    return true;
  }

where key is coming from keystroke. (like 'a', '6' , '.' etc) What should be the value of key for if condition to return true?

I tried key as '.' since it is a word boundary (But only with a word, sadly). What should be the key to show backspace <-.

Asim K T
  • 16,864
  • 10
  • 77
  • 99
  • 1
    you mean this ? `key.match(/[\w.]/)` – Avinash Raj Apr 11 '16 at 12:15
  • No, It will match any word (word character). I want to match a word boundary. They are different. http://stackoverflow.com/questions/11874234/difference-between-w-and-b-regular-expression-meta-characters – Asim K T Apr 11 '16 at 12:21
  • 1
    You can't detect a word boundary without at least two characters to compare. Testing `\b` against a character always is *true*. Against a space = false. Also, your regex tests to see if a backspace character is present, not a word boundary. Check [here at regex1010](https://regex101.com/r/zI1rV5/1) - explanation in the right pane. – SamWhan Apr 11 '16 at 12:24
  • `[\b]` matches a backspace character since `\b` is inside a character class. To match a word boundary, use `/\b/`. @ClasG: You can return true with just 1 character - but it must be a word character. See [demo](https://regex101.com/r/lB3wN0/1) – Wiktor Stribiżew Apr 11 '16 at 12:48
  • I thought backspace is also a word boundary. Actually I needed to test the key event for backspace since in FF I can't delete typed chars. Updated the question and thanks for correcting me. @ClasG – Asim K T Apr 11 '16 at 13:20

1 Answers1

3

[\b] matches a backspace character since \b is inside a character class. To match a word boundary, use /\b/ (where \b is not inside a character class):

var key = "a";
if (/\b/.test(key)) { // Or, perhaps, for better portability, /[\x08]/.test(key)
    alert(true);
}

If you plan to match all non-word characters you just need \W, not \b. The word boundary \b matches a position between ^ and \w or \w and $, or \W and \w or \w and \W (where \w stands for a [a-zA-Z0-9_] and \W stands for any other character not inside these ranges). Also, if you need to match a 1-char string, do not forget anchors: /^\W$/.

UPDATE

To test a backspace key, you can use

var key = "\x08";
if (/[\b]/.test(key)) {
 alert(true);
}
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • I needed to match the backspace. Updated the question. Upvoted answer for correcting me for mistaking word boundary and backspace. Thought backspace is also a word boundary. – Asim K T Apr 11 '16 at 13:23
  • I guess the answer is just what you need then :) Or do you want to delete your post now? – Wiktor Stribiżew Apr 11 '16 at 13:26
  • No. The code is working. When it is backspace it is returning true. I want to mock the behavior. Means, what value should I manually add to variable `key` so that it will return true? if it's `.` , `a` etc it will return false. I am unit-testing the function. – Asim K T Apr 11 '16 at 13:31
  • I guess `/[\b]/.test("\x08")` – Wiktor Stribiżew Apr 11 '16 at 13:33
  • That's a good way to go. I suppose it have good browser support. (Phones also have the same unicodes, right?) – Asim K T Apr 11 '16 at 13:37
  • That is a good point: maybe you should use `/[\x08]/.test(key)` for better portability. I do not remember right now where a problem can arise, but using the `\x` notation may be helpful in general. – Wiktor Stribiżew Apr 11 '16 at 13:40