重音字符(变音符号)的具体 Javascript 正则表达式 [英] Concrete Javascript Regex for Accented Characters (Diacritics)

查看:37
本文介绍了重音字符(变音符号)的具体 Javascript 正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看过堆栈溢出(替换字符..嗯, JavaScript 如何不遵循有关 RegExp 的 Unicode 标准 等)并且还没有真正找到问题的具体答案:

I've looked on Stack Overflow (replacing characters.. eh, how JavaScript doesn't follow the Unicode standard concerning RegExp, etc.) and haven't really found a concrete answer to the question:

JavaScript 如何匹配重音字符(带有变音符号的字符)?

我强制 UI 中的字段匹配以下格式:last_name, first_name (last [comma space] first),我想为以下内容提供支持变音符号,但显然在 JavaScript 中它比其他语言/平台更难.

I'm forcing a field in a UI to match the format: last_name, first_name (last [comma space] first), and I want to provide support for diacritics, but evidently in JavaScript it's a bit more difficult than other languages/platforms.

这是我的原始版本,直到我想添加变音符号支持:

This was my original version, until I wanted to add diacritic support:

/^[a-zA-Z]+,s[a-zA-Z]+$/

目前我正在讨论添加支持的三种方法中的一种,所有这些方法我都已经测试并有效(至少在某种程度上,我真的不知道第二种方法的程度"是什么).他们在这里:

Currently I'm debating one of three methods to add support, all of which I have tested and work (at least to some extent, I don't really know what the "extent" is of the second approach). Here they are:

var accentedCharacters = "àèìòùÀÈÌÒÙáéíóúýÁÉÍÓÚÝâêîôûÂÊÎÔÛãñõÃÑÕäëïöüÿÄËÏÖÜŸçÇßØøÅåÆæœ";
// Build the full regex
var regex = "^[a-zA-Z" + accentedCharacters + "]+,\s[a-zA-Z" + accentedCharacters + "]+$";
// Create a RegExp from the string version
regexCompiled = new RegExp(regex);
// regexCompiled = /^[a-zA-ZàèìòùÀÈÌÒÙáéíóúýÁÉÍÓÚÝâêîôûÂÊÎÔÛãñõÃÑÕäëïöüÿÄËÏÖÜŸçÇßØøÅåÆæœ]+,s[a-zA-ZàèìòùÀÈÌÒÙáéíóúýÁÉÍÓÚÝâêîôûÂÊÎÔÛãñõÃÑÕäëïöüÿÄËÏÖÜŸçÇßØøÅåÆæœ]+$/

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆