How can I check if variable contains Chinese/Japanese characters?

前端 未结 2 578
既然无缘
既然无缘 2020-12-29 15:34

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]/))


        
相关标签:
2条回答
  • 2020-12-29 15:39

    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/

    0 讨论(0)
  • 2020-12-29 15:47

    .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(...).

    0 讨论(0)
提交回复
热议问题