9

How do I check if a variable contains Chinese or Japanese characters? I know that this line works:

if (document.body.innerText.match(/[\u3400-\u9FBF]/))

I need to do the same thing not for the document but for a single variable.

user229044
  • 232,980
  • 40
  • 330
  • 338
waivy
  • 127
  • 1
  • 2
  • 7
  • 4
    `if (variable.match(/[\u3400-\u9FBF]/))` ? – Esailija Jun 26 '12 at 11:35
  • You can apply `.match` to *any* string variable, not just `document.body.innerText`. – user229044 Jun 26 '12 at 11:35
  • Thank you. What I'm trying to do is to have the user select a portion of text from the document and assign it to my variable and after this check if the variable contains any Chinese character. I'm using var sel = window.getSelection() and then if (sel.match(/[\u3400-\u9FBF]/)) but it won't work. Strange thing is that if e.g I manually set sel = 漢字 it works fine but if I try to get it with window.getSelection() it won't – waivy Jun 26 '12 at 12:15

2 Answers2

8

.match is a string method. You can apply it to anything that contains string. And, of course, to arbitrary variable.

In case you have something that is not string, most objects define .toString() method that converts its content to some reasonable stringified form. When you retrieve selection from page, you get selection object. Convert it to string and then use match on it: sel.toString().match(...).

Tim Down
  • 318,141
  • 75
  • 454
  • 536
Oleg V. Volkov
  • 21,719
  • 4
  • 44
  • 68
  • Thank you. What I'm trying to do is to have the user select a portion of text from the document and assign it to my variable and after this check if the variable contains any Chinese character. I'm using var sel = window.getSelection() and then if (sel.match(/[\u3400-\u9FBF]/)) but it won't work. Strange thing is that if e.g I manually set sel = 漢字 it works fine but if I try to get it with window.getSelection() it won't. – waivy Jun 26 '12 at 12:12
  • 1
    That's because it returns selection object, that must be first converted to string. Use `sel.toString().match(...)`. – Oleg V. Volkov Jun 26 '12 at 12:18
  • 3
    `/[\u3400-\u9FBF]/.test( window.getSelection().toString() )` would be better: it only has to return a Boolean rather than an array of matches. – Tim Down Jun 26 '12 at 13:10
0

afaik you can to the same with a variable... document.body.innerText just returns the text of the body. Therefore you can just do

myvar.match(...)

Here's an example: http://snipplr.com/view/15357/

Atmocreations
  • 9,923
  • 15
  • 67
  • 102
  • Thank you. What I'm trying to do is to have the user select a portion of text from the document and assign it to my variable and after this check if the variable contains any Chinese character. I'm using var sel = window.getSelection() and then if (sel.match(/[\u3400-\u9FBF]/)) but it won't work. Strange thing is that if e.g I manually set sel = 漢字 it works fine but if I try to get it with window.getSelection() it won't. – waivy Jun 26 '12 at 12:13
  • try `sel.toString().match(/[\u3400-\u9FBF]/))` – Atmocreations Jun 27 '12 at 14:36