简体   繁体   English

支持Unicode字母的JavaScript utf8编码或正则表达式模式?

[英]JavaScript utf8 encoding or regex pattern with Unicode letters support?

I have been recently requested, to adapt an app's input, to support Unicode letters, on some of the inputs within the web app. 我最近被要求调整应用程序的输入,以支持Web应用程序中的一些输入上的Unicode字母。

That app, already does some validation with regex, with the pattern html attribute. 该应用程序已经使用pattern html属性对正则表达式进行了一些验证。 Like so: 像这样:

<input required="true" pattern="[a-zA-Z0-9_\-]+" type="text" name="name">

Now, since I have to adapt some inputs to the new requirements, I was wondering what would be better to do? 现在,由于我必须根据新要求调整一些输入,我想知道最好的做法是什么?

or 要么

Javascript is by definition completely in unicode (Except websites with non unicode encoding, but there the solution may still work), so just add letters you need to regexp. 根据定义,Javascript完全是unicode(除了具有非unicode编码的网站,但解决方案可能仍然有效),所以只需添加regexp所需的字母。 If you need to add them by charcode use \\x0000 如果需要通过charcode添加它们,请使用\\x0000

I had decided to go for editing the regex option, since when my view starts being processed, there is a module that will set the pattern attribute, with the defined regex, on specific inputs. 我决定去编辑正则表达式选项,因为当我的view开始被处理时,有一个模块将使用定义的正则表达式在特定输入上设置pattern属性。

So, it is better to edit the regex pattern and then set in on inputs when the view is loaded, then doing: 因此,最好编辑正则表达式模式,然后在加载视图时设置输入,然后执行:

  1. Load view and set pattern attributes for each input 加载视图并为每个输入设置模式属性
  2. Write js to analyze specific inputs, to see if they contain decoded Unicode characters 编写js以分析特定输入,以查看它们是否包含已解码的Unicode字符
  3. If so, encode those characters while typing/before submit, since I have a regex pattern that doesn't allow such characters 如果是这样,在输入之前/在提交之前编码这些字符,因为我有一个不允许这样的字符的正则表达式模式

Essentially, I'm saving me time on writing useless code, and most important, a lot of browser processing (it was going to be to much, for what it is needed). 从本质上讲,我节省了编写无用代码的时间,最重要的是,大量的浏览器处理(对于需要的东西来说,它将会很多)。

I should've gone for this option since the beginning (duh!) 我从一开始就应该选择这个选项(呃!)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM