如何从字符串中删除突出的字符?特别是在IE6中,我有这样的事情:
accentsTidy = function(s){
var r=s.toLowerCase();
r = r.replace(new RegExp(/\s/g),"");
r = r.replace(new RegExp(/[àáâãäå]/g),"a");
r = r.replace(new RegExp(/æ/g),"ae");
r = r.replace(new RegExp(/ç/g),"c");
r = r.replace(new RegExp(/[èéêë]/g),"e");
r = r.replace(new RegExp(/[ìíîï]/g),"i");
r = r.replace(new RegExp(/ñ/g),"n");
r = r.replace(new RegExp(/[òóôõö]/g),"o");
r = r.replace(new RegExp(/œ/g),"oe");
r = r.replace(new RegExp(/[ùúûü]/g),"u");
r = r.replace(new RegExp(/[ýÿ]/g),"y");
r = r.replace(new RegExp(/\W/g),"");
return r;
};
Run Code Online (Sandbox Code Playgroud)
但IE6让我烦恼,似乎它不喜欢我的正则表达式.
我看过Stack Overflow(替换字符......呃,JavaScript如何不遵循关于RegExp的Unicode标准等)并且没有真正找到问题的具体答案:
How can JavaScript match for accented characters (those with diacritical marks)?
我强迫UI中的字段匹配格式:( last_name, first_name 最后[逗号空间]),我想提供对变音符号的支持,但显然在JavaScript中它比其他语言/平台要困难一些.
这是我的原始版本,直到我想添加变音支持:
/^[a-zA-Z]+,\s[a-zA-Z]+$/
目前我正在讨论增加支持的三种方法之一,所有这些我都经过测试和工作(至少在某种程度上,我真的不知道第二种方法的"范围"是什么).他们来了:
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àèìòùÀÈÌÒÙáéíóúýÁÉÍÓÚÝâêîôûÂÊÎÔÛãñõÃÑÕäëïöüÿÄËÏÖÜŸçÇߨøÅ寿œ]+$/
Run Code Online (Sandbox Code Playgroud)
accentedCharacters..字符类,以获得更简单的表达式:var regex = /^.+,\s.+$/;
Run Code Online (Sandbox Code Playgroud)
something, something.那我觉得好吧......对于一个穷人在客户端实现近似校正正确的排序,我需要一个JavaScript函数来在字符串中进行有效的单个字符替换.
这就是我的意思(请注意,这适用于德语文本,其他语言的排序方式不同):
native sorting gets it wrong: a b c o u z ä ö ü collation-correct would be: a ä b c o ö u ü z
基本上,我需要将所有出现的给定字符串的"ä"替换为"a"(依此类推).这样,本机排序的结果将非常接近用户期望的结果(或数据库将返回的内容).
其他语言有这样的设施:Python提供str.translate(),在Perl中tr/…/…/,XPath有一个函数translate(),ColdFusion有ReplaceList().但是JavaScript呢?
这就是我现在所拥有的.
// s would be a rather short string (something like
// 200 characters at max, most of the time much less)
function makeSortString(s) {
var translate = {
"ä": "a", "ö": "o", "ü": …Run Code Online (Sandbox Code Playgroud) 我需要删除西班牙语中的重音和来自不同数据集的其他语言的重音.
我已经在这篇文章提供的代码中做了一个函数,删除了特殊的重音符号.问题是函数很慢,因为它使用了UDF.我只是想知道我是否可以提高函数的性能以在更短的时间内获得结果,因为这对小型数据帧有好处,但对大型数据帧则不行.
提前致谢.
在这里代码,您将能够按照它呈现的方式运行它:
# Importing sql types
from pyspark.sql.types import StringType, IntegerType, StructType, StructField
from pyspark.sql.functions import udf, col
import unicodedata
# Building a simple dataframe:
schema = StructType([StructField("city", StringType(), True),
StructField("country", StringType(), True),
StructField("population", IntegerType(), True)])
countries = ['Venezuela', 'US@A', 'Brazil', 'Spain']
cities = ['Maracaibó', 'New York', ' São Paulo ', '~Madrid']
population = [37800000,19795791,12341418,6489162]
# Dataframe:
df = sqlContext.createDataFrame(list(zip(cities, countries, population)), schema=schema)
df.show()
class Test():
def __init__(self, df):
self.df = df
def clearAccents(self, …Run Code Online (Sandbox Code Playgroud) python unicode-normalization apache-spark apache-spark-sql pyspark
jquery是否具有参数化方法,如rails参数化?我想使用jQuery参数化字符串,例如:
"Jonh [ Doe ] " => "john-doe"
" John ( Doe )" => "john-doe"
"{ John } Doe / " => "john-doe"
Run Code Online (Sandbox Code Playgroud)
我知道,可以用toLowerCase()和.replace(),但我想最好的做法去做.现在我使用这个脚本
toLowerCase().replace(/\[|\]|\(|\)|\{|\}|\\|\//g, '').replace(/\s/g, '-')
Run Code Online (Sandbox Code Playgroud)
要么
toLowerCase().replace(/[^a-z0-9\s]/gi, '').replace(/[_\s]/g, '-')
Run Code Online (Sandbox Code Playgroud)
但结果看起来像这样
"{ John } Doe / " => "-john--doe--"
Run Code Online (Sandbox Code Playgroud) 我正在尝试为Google表格编写一个公式,它会将带有变音符号的Unicode字符转换为纯ASCII等效字符.
我看到Google在其"REGEXREPLACE"功能中使用了RE2.我发现RE2提供了Unicode字符类.
我试着写一个公式(类似于这个):
REGEXREPLACE("público","(\pL)\pM*","$1")
Run Code Online (Sandbox Code Playgroud)
但是Sheets会产生以下错误:
函数REGEXREPLACE参数2值"\ pL"不是有效的正则表达式.
我想我可以写一个由一组很长的嵌套SUBSTITUTE函数组成的公式(就像这个一样),但这看起来非常糟糕.
是否可以提供更好的方法来建议使用Google表格公式中的带有变音/重音标记的Unicode字母标准化?
javascript ×4
regex ×2
unicode ×2
apache-spark ×1
collation ×1
diacritics ×1
formulas ×1
jquery ×1
pyspark ×1
python ×1
re2 ×1
sorting ×1
string ×1