Pourquoi ne puis-je utiliser des caractères accentués à côté d'une limite de mot?
-
20-09-2019 - |
Question
Je suis en train de faire une regex dynamique qui correspond au nom d'une personne. Il fonctionne sans problème sur la plupart des noms, jusqu'à ce que je courais en caractères accentués à la fin du nom.
Exemple: Certains Fantaisie nom
Le regex Je l'ai utilisé jusqu'à présent est:
/\b(Fancy Namé|Namé)\b/i
Utilisé comme ceci:
"Goal: Some Fancy Namé. Awesome.".replace(/\b(Fancy Namé|Namé)\b/i, '<a href="#">$1</a>');
tout simplement pas égaler. Si je remplace l'é avec un e, il correspond très bien. Si je tente de faire correspondre un nom tel que « Certains Fancy nomA », cela fonctionne très bien. Si je supprime le dernier mot d'ancrage limite de mot, il fonctionne très bien.
Pourquoi ne pas le drapeau frontière mot travail ici? Toutes les suggestions sur la façon dont je contourner ce problème?
Je l'ai envisagé d'utiliser quelque chose comme ça, mais je ne suis pas sûr de ce que les pénalités de performance seraient comme:
"Some fancy namé. Allow me to ellaborate.".replace(/([\s.,!?])(fancy namé|namé)([\s.,!?]|$)/g, '$1<a href="#">$2</a>$3')
Suggestions? Idées?
La solution
La mise en œuvre de regex JavaScript ne sont pas compatibles Unicode. Il ne connaît que les « caractères de mots » dans la norme ASCII à faible octet, qui ne comprend pas é
ou d'autres lettres accentuées ou non anglais.
Parce que é
est pas un caractère de mot à JS, é
suivi d'un espace ne peut jamais être considéré comme une limite de mot. (Il correspondrait \b
si elle est utilisée au milieu d'un mot, comme Namés
.)
/([\s.,!?])(fancy namé|namé)([\s.,!?]|$)/
Ouais, ce serait la solution habituelle pour JS (mais probablement avec plus de caractères de ponctuation). Pour les autres langues que vous souhaitez généralement utiliser préanalyse / lookbehind pour éviter le correspondant avant et après les caractères limites, mais ceux-ci sont mal pris en charge / buggy JS donc mieux éviter.
Autres conseils
Rob est correct. Cité de la 3ème édition ECMAScript:
15.10.2.6 Assertion:
La production Assertion
\b
évalue par ...2. Appeler IsWordChar (e-1) et laisser a être le résultat booléen
3. Appeler IsWordChar (e) et laisser b le résultat booléen
et
La fonction d'aide interne IsWordChar ... effectue les opérations suivantes:
3. Si c est l'un des soixante-trois caractères dans le tableau ci-dessous, le retour true .
a b c d e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 0 1 2 3 4 5 6 7 8 9 _
Depuis é
ne fait pas partie de ces 63 caractères, l'emplacement entre é
et a
sera considérée comme une limite de mot.
Si vous connaissez la classe de caractères, vous pouvez utiliser une assertion de regard avant-négatif, par exemple.
/(^|[^\wÀ-ÖØ-öø-ſ])(Fancy Namé|Namé)(?![\wÀ-ÖØ-öø-ſ])/
Connaissez vos limites
Malheureusement, même si et quand Javascript doit venir un jour d'avoir un soutien adéquat et intégral Unicode, vous toujours Il faut être délicieusement prudent avec les limites de mots. Il est facile de mal comprendre ce qu'est un \b
fait vraiment.
Voici le code Perl qui explique ce \b
est vraiment en train de faire, ce qui est vrai, peu importe si votre moteur de modèle a été mis à jour BNM-encore:
# if next is word char:
# then last isn't word
# else last isn't nonword
$word_boundary_before = qr{ (?(?= \w ) (?<! \w ) | (?<! \W ) ) }x;
# if last is word:
# then next isn't word
# else next isn't nonword
$word_boundary_after = qr{ (?(?<= \w ) (?! \w ) | (?! \W ) ) }x;
La première est comme un \b
avant que quelque chose, et le second est comme un \b
après. Le produit d'assemblage utilisé est l'expression rationnelle « IF-THEN = ELSE » conditionnelle, ce qui est de la forme générale (?(COND)THEN|ELSE)
. J'utilise ici COND test est un test avant dans le premier cas mais dans le second test avant celui-ci. ALORS et ELSE clauses dans les deux cas sont annulées lookarounds pour qu'ils prennent le bord de la chaîne en compte.
J'explique plus sur le traitement des frontières et Unicode dans les expressions régulières ici .
Support Unicode Propriété
Le état actuel des choses dans le traitement Javascript Unicode semble être que comme Java, les définitions de Javascript de \w
et tels sont encore estropiés par être coincé dans les années 1960 monde ASCII. Ceci est juste une situation misérable, je l'avoue. Même Python, ce qui est assez prudent car ces choses vont (par exemple, il ne supporte même pas regexes récursifs), Finalité permettent ses définitions de \w
et \s
de travailler correctement sur Unicode. C'est le plus strict minimum de fonctionnalité, vraiment.
Il est à la fois meilleur et le pire dans JavasScript. C'est bcecause vous peut utiliser quelques-unes des très les plus élémentaires des propriétés Unicode en Javascript (ou Java). Il semble que vous devriez être en mesure d'utiliser les une ou deux caractères propriétés Unicode « Général Catégorie ». Cela signifie que vous devriez être en mesure d'utiliser les versions courtes de nom de la première colonne ci-dessous:
Short Name Long Name
¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
\pL \p{Letter}
\p{Lu} \p{Uppercase_Letter}
\p{Ll} \p{Lowercase_Letter}
\p{Lt} \p{Titlecase_Letter}
\p{Lm} \p{Modifier_Letter}
\p{Lo} \p{Other_Letter}
Short Name Long Name
¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
\pM \p{Mark}
\p{Mn} \p{Nonspacing_Mark}
\p{Mc} \p{Spacing_Mark}
\p{Me} \p{Enclosing_Mark}
Short Name Long Name
¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
\pN \p{Number}
\p{Nd} \p{Decimal_Number},\p{Digit}
\p{Nl} \p{Letter_Number}
\p{No} \p{Other_Number}
Short Name Long Name
¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
\pP \p{Punctuation}, \p{Punct})
\p{Pc} \p{Connector_Punctuation}
\p{Pd} \p{Dash_Punctuation}
\p{Ps} \p{Open_Punctuation}
\p{Pe} \p{Close_Punctuation}
\p{Pi} \p{Initial_Punctuation}
\p{Pf} \p{Final_Punctuation}
\p{Po} \p{Other_Punctuation}
Short Name Long Name
¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
\pS \p{Symbol}
\p{Sm} \p{Math_Symbol}
\p{Sc} \p{Currency_Symbol}
\p{Sk} \p{Modifier_Symbol}
\p{So} \p{Other_Symbol}
Short Name Long Name
¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
\pZ \p{Separator}
\p{Zs} \p{Space_Separator}
\p{Zl} \p{Line_Separator}
\p{Zp} \p{Paragraph_Separator}
Short Name Long Name
¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
\pC \p{Other}
\p{Cc} \p{Control}, \p{Cntrl}
\p{Cf} \p{Format}
\p{Cs} \p{Surrogate}
\p{Co} \p{Private_Use}
\p{Cn} \p{Unassigned}
Vous devez utiliser les noms courts que dans Java et Javascript, mais Perl vous permet d'utiliser les noms longs aussi, ce qui contribue à legibilityl 5,12 version de Perl prend en charge environ 3000 propriétés Unicode. Python toujours n'a pas le soutien de la propriété Unicode convient de mentionner, et Ruby commence à peine à l'obtenir dans le 1.9 version. PCRE a un soutien limité, la plupart du temps comme Java 1.7 de.
java6 prend en charge les propriétés du bloc Unicode, comme \p{InGeneralPunctuation}
ou \p{Block=GeneralPunctuation}
et java7 supporte les propriétés de script Unicode, comme \p{IsHiragana}
ou \p{Script=Hiragana}
.
Cependant, il ne supporte toujours pas quoi que ce soit, même à proximité du ensemble complet de propriétés Unicode , y compris les quasi-critiques comme \p{WhiteSpace}
, \p{Dash}
et \p{Quotation_Mark}
, sans parler des deux autres parters comme \p{Line_Break=Alphabetic}
, \p{East_Asian_Width:Narrow}
, \p{Numeric_Value=1000}
ou \p{Age:5.2}
.
L'ancien ensemble sont assez indispensables - en particulier étant donné le manque de soutien \s
de travail à droite -. Et le second ensemble sont assez sacrément utiles parfois
Quelque chose d'autre que Java et Javascript ne supporte pas encore est utilisateur propriétés de caractère -defined. J'utilise les un peu. De cette façon, vous pouvez définir des choses comme \p{English::Vowel}
ou \p{English::Consonant}
, ce qui est tout à fait à portée de main.
Si vous êtes intéressé par les propriétés Unicode pour le travail regex, Pourrait vouloir Tou saisir la unitrio série de programmes: uniprops , unichars et UniNames . Voici une démo de chacun de ces trois:
$ uninames face
፦ 4966 1366 ETHIOPIC PREFACE COLON
⁙ 8281 2059 FIVE DOT PUNCTUATION
= Greek pentonkion
= quincunx
x (die face-5 - 2684)
∯ 8751 222F SURFACE INTEGRAL
# 222E 222E
☹ 9785 2639 WHITE FROWNING FACE
☺ 9786 263A WHITE SMILING FACE
= have a nice day!
☻ 9787 263B BLACK SMILING FACE
⚀ 9856 2680 DIE FACE-1
⚁ 9857 2681 DIE FACE-2
⚂ 9858 2682 DIE FACE-3
⚃ 9859 2683 DIE FACE-4
⚄ 9860 2684 DIE FACE-5
⚅ 9861 2685 DIE FACE-6
⾯ 12207 2FAF KANGXI RADICAL FACE
# 9762
〠 12320 3020 POSTAL MARK FACE
龜 64206 FACE CJK COMPATIBILITY IDEOGRAPH-FACE
: 9F9C
FMTEYEWTK sur les propriétés Unicode:
$ uniprops -va LF 85 Greek:Sigma INFINITY BOM U+3000 U+12345
U+000A ‹U+000A› \N{ LINE FEED (LF) }:
\s \v \R \pC \p{Cc}
\p{All} \p{Any} \p{ASCII} \p{Assigned} \p{C} \p{Other} \p{Cc} \p{Cntrl} \p{Common} \p{Zyyy} \p{Control} \p{Pat_WS} \p{Pattern_White_Space} \p{PatWS} \p{PerlSpace} \p{PosixCntrl} \p{PosixSpace} \p{Space} \p{SpacePerl} \p{VertSpace} \p{White_Space} \p{WSpace}
\p{Age:1.1} \p{Block=Basic_Latin} \p{Bidi_Class:B} \p{Bidi_Class=Paragraph_Separator} \p{Bidi_Class:Paragraph_Separator} \p{Bc=B} \p{Block:ASCII} \p{Block:Basic_Latin} \p{Blk=ASCII} \p{Canonical_Combining_Class:0} \p{Canonical_Combining_Class=Not_Reordered}
\p{Canonical_Combining_Class:Not_Reordered} \p{Ccc=NR} \p{Canonical_Combining_Class:NR} \p{Script=Common} \p{Decomposition_Type:None} \p{Dt=None} \p{East_Asian_Width=Neutral} \p{East_Asian_Width:Neutral} \p{Grapheme_Cluster_Break:LF} \p{GCB=LF} \p{Hangul_Syllable_Type:NA}
\p{Hangul_Syllable_Type=Not_Applicable} \p{Hangul_Syllable_Type:Not_Applicable} \p{Hst=NA} \p{Joining_Group:No_Joining_Group} \p{Jg=NoJoiningGroup} \p{Joining_Type:Non_Joining} \p{Jt=U} \p{Joining_Type:U} \p{Joining_Type=Non_Joining} \p{Line_Break:LF} \p{Line_Break=Line_Feed}
\p{Line_Break:Line_Feed} \p{Lb=LF} \p{Numeric_Type:None} \p{Nt=None} \p{Numeric_Value:NaN} \p{Nv=NaN} \p{Present_In:1.1} \p{Age=1.1} \p{In=1.1} \p{Present_In:2.0} \p{In=2.0} \p{Present_In:2.1} \p{In=2.1} \p{Present_In:3.0} \p{In=3.0} \p{Present_In:3.1} \p{In=3.1}
\p{Present_In:3.2} \p{In=3.2} \p{Present_In:4.0} \p{In=4.0} \p{Present_In:4.1} \p{In=4.1} \p{Present_In:5.0} \p{In=5.0} \p{Present_In:5.1} \p{In=5.1} \p{Present_In:5.2} \p{In=5.2} \p{Script:Common} \p{Sc=Zyyy} \p{Script:Zyyy} \p{Sentence_Break:LF} \p{SB=LF} \p{Word_Break:LF}
\p{WB=LF}
U+0085 ‹U+0085› \N{ NEXT LINE (NEL) }:
\s \v \R \pC \p{Cc}
\p{All} \p{Any} \p{Assigned} \p{InLatin1} \p{C} \p{Other} \p{Cc} \p{Cntrl} \p{Common} \p{Zyyy} \p{Control} \p{Pat_WS} \p{Pattern_White_Space} \p{PatWS} \p{Space} \p{SpacePerl} \p{VertSpace} \p{White_Space} \p{WSpace}
\p{Age:1.1} \p{Bidi_Class:B} \p{Bidi_Class=Paragraph_Separator} \p{Bidi_Class:Paragraph_Separator} \p{Bc=B} \p{Block:Latin_1} \p{Block=Latin_1_Supplement} \p{Block:Latin_1_Supplement} \p{Blk=Latin1} \p{Canonical_Combining_Class:0} \p{Canonical_Combining_Class=Not_Reordered}
\p{Canonical_Combining_Class:Not_Reordered} \p{Ccc=NR} \p{Canonical_Combining_Class:NR} \p{Script=Common} \p{Decomposition_Type:None} \p{Dt=None} \p{East_Asian_Width=Neutral} \p{East_Asian_Width:Neutral} \p{Grapheme_Cluster_Break:CN} \p{Grapheme_Cluster_Break=Control}
\p{Grapheme_Cluster_Break:Control} \p{GCB=CN} \p{Hangul_Syllable_Type:NA} \p{Hangul_Syllable_Type=Not_Applicable} \p{Hangul_Syllable_Type:Not_Applicable} \p{Hst=NA} \p{Joining_Group:No_Joining_Group} \p{Jg=NoJoiningGroup} \p{Joining_Type:Non_Joining} \p{Jt=U}
\p{Joining_Type:U} \p{Joining_Type=Non_Joining} \p{Line_Break:Next_Line} \p{Lb=NL} \p{Line_Break:NL} \p{Line_Break=Next_Line} \p{Numeric_Type:None} \p{Nt=None} \p{Numeric_Value:NaN} \p{Nv=NaN} \p{Present_In:1.1} \p{Age=1.1} \p{In=1.1} \p{Present_In:2.0} \p{In=2.0}
\p{Present_In:2.1} \p{In=2.1} \p{Present_In:3.0} \p{In=3.0} \p{Present_In:3.1} \p{In=3.1} \p{Present_In:3.2} \p{In=3.2} \p{Present_In:4.0} \p{In=4.0} \p{Present_In:4.1} \p{In=4.1} \p{Present_In:5.0} \p{In=5.0} \p{Present_In:5.1} \p{In=5.1} \p{Present_In:5.2} \p{In=5.2}
\p{Script:Common} \p{Sc=Zyyy} \p{Script:Zyyy} \p{Sentence_Break:SE} \p{Sentence_Break=Sep} \p{Sentence_Break:Sep} \p{SB=SE} \p{Word_Break:Newline} \p{WB=NL} \p{Word_Break:NL} \p{Word_Break=Newline}
U+03A3 ‹Σ› \N{ GREEK CAPITAL LETTER SIGMA }:
\w \pL} \p{LC} \p{L_} \p{L&} \p{Lu}
\p{All} \p{Any} \p{Alnum} \p{Alpha} \p{Alphabetic} \p{Assigned} \p{Greek} \p{Is_Greek} \p{InGreek} \p{Cased} \p{Cased_Letter} \p{LC} \p{Changes_When_Casefolded} \p{CWCF} \p{Changes_When_Casemapped} \p{CWCM} \p{Changes_When_Lowercased} \p{CWL} \p{Changes_When_NFKC_Casefolded}
\p{CWKCF} \p{Lu} \p{L} \p{Gr_Base} \p{Grapheme_Base} \p{Graph} \p{GrBase} \p{Grek} \p{Greek_And_Coptic} \p{ID_Continue} \p{IDC} \p{ID_Start} \p{IDS} \p{Letter} \p{L_} \p{Uppercase_Letter} \p{Print} \p{Upper} \p{Uppercase} \p{Word} \p{XID_Continue} \p{XIDC} \p{XID_Start}
\p{XIDS}
\p{Age:1.1} \p{Bidi_Class:L} \p{Bidi_Class=Left_To_Right} \p{Bidi_Class:Left_To_Right} \p{Bc=L} \p{Block:Greek} \p{Block=Greek_And_Coptic} \p{Block:Greek_And_Coptic} \p{Blk=Greek} \p{Canonical_Combining_Class:0} \p{Canonical_Combining_Class=Not_Reordered}
\p{Canonical_Combining_Class:Not_Reordered} \p{Ccc=NR} \p{Canonical_Combining_Class:NR} \p{Decomposition_Type:None} \p{Dt=None} \p{East_Asian_Width:A} \p{East_Asian_Width=Ambiguous} \p{East_Asian_Width:Ambiguous} \p{Ea=A} \p{Grapheme_Cluster_Break:Other} \p{GCB=XX}
\p{Grapheme_Cluster_Break:XX} \p{Grapheme_Cluster_Break=Other} \p{Script=Greek} \p{Hangul_Syllable_Type:NA} \p{Hangul_Syllable_Type=Not_Applicable} \p{Hangul_Syllable_Type:Not_Applicable} \p{Hst=NA} \p{Joining_Group:No_Joining_Group} \p{Jg=NoJoiningGroup}
\p{Joining_Type:Non_Joining} \p{Jt=U} \p{Joining_Type:U} \p{Joining_Type=Non_Joining} \p{Line_Break:AL} \p{Line_Break=Alphabetic} \p{Line_Break:Alphabetic} \p{Lb=AL} \p{Numeric_Type:None} \p{Nt=None} \p{Numeric_Value:NaN} \p{Nv=NaN} \p{Present_In:1.1} \p{Age=1.1} \p{In=1.1}
\p{Present_In:2.0} \p{In=2.0} \p{Present_In:2.1} \p{In=2.1} \p{Present_In:3.0} \p{In=3.0} \p{Present_In:3.1} \p{In=3.1} \p{Present_In:3.2} \p{In=3.2} \p{Present_In:4.0} \p{In=4.0} \p{Present_In:4.1} \p{In=4.1} \p{Present_In:5.0} \p{In=5.0} \p{Present_In:5.1} \p{In=5.1}
\p{Present_In:5.2} \p{In=5.2} \p{Script:Greek} \p{Sc=Grek} \p{Script:Grek} \p{Sentence_Break:UP} \p{Sentence_Break=Upper} \p{Sentence_Break:Upper} \p{SB=UP} \p{Word_Break:ALetter} \p{WB=LE} \p{Word_Break:LE} \p{Word_Break=ALetter}
U+221E ‹∞› \N{ INFINITY }:
\pS \p{Sm}
\p{All} \p{Any} \p{Assigned} \p{InMathematicalOperators} \p{Common} \p{Zyyy} \p{Sm} \p{S} \p{Gr_Base} \p{Grapheme_Base} \p{Graph} \p{GrBase} \p{Math} \p{Math_Symbol} \p{Pat_Syn} \p{Pattern_Syntax} \p{PatSyn} \p{Print} \p{Symbol}
\p{Age:1.1} \p{Bidi_Class:ON} \p{Bidi_Class=Other_Neutral} \p{Bidi_Class:Other_Neutral} \p{Bc=ON} \p{Block:Mathematical_Operators} \p{Canonical_Combining_Class:0} \p{Canonical_Combining_Class=Not_Reordered} \p{Canonical_Combining_Class:Not_Reordered} \p{Ccc=NR}
\p{Canonical_Combining_Class:NR} \p{Script=Common} \p{Decomposition_Type:None} \p{Dt=None} \p{East_Asian_Width:A} \p{East_Asian_Width=Ambiguous} \p{East_Asian_Width:Ambiguous} \p{Ea=A} \p{Grapheme_Cluster_Break:Other} \p{GCB=XX} \p{Grapheme_Cluster_Break:XX}
\p{Grapheme_Cluster_Break=Other} \p{Hangul_Syllable_Type:NA} \p{Hangul_Syllable_Type=Not_Applicable} \p{Hangul_Syllable_Type:Not_Applicable} \p{Hst=NA} \p{Joining_Group:No_Joining_Group} \p{Jg=NoJoiningGroup} \p{Joining_Type:Non_Joining} \p{Jt=U} \p{Joining_Type:U}
\p{Joining_Type=Non_Joining} \p{Line_Break:AI} \p{Line_Break=Ambiguous} \p{Line_Break:Ambiguous} \p{Lb=AI} \p{Numeric_Type:None} \p{Nt=None} \p{Numeric_Value:NaN} \p{Nv=NaN} \p{Present_In:1.1} \p{Age=1.1} \p{In=1.1} \p{Present_In:2.0} \p{In=2.0} \p{Present_In:2.1} \p{In=2.1}
\p{Present_In:3.0} \p{In=3.0} \p{Present_In:3.1} \p{In=3.1} \p{Present_In:3.2} \p{In=3.2} \p{Present_In:4.0} \p{In=4.0} \p{Present_In:4.1} \p{In=4.1} \p{Present_In:5.0} \p{In=5.0} \p{Present_In:5.1} \p{In=5.1} \p{Present_In:5.2} \p{In=5.2} \p{Script:Common} \p{Sc=Zyyy}
\p{Script:Zyyy} \p{Sentence_Break:Other} \p{SB=XX} \p{Sentence_Break:XX} \p{Sentence_Break=Other} \p{Word_Break:Other} \p{WB=XX} \p{Word_Break:XX} \p{Word_Break=Other}
U+FEFF ‹U+FEFF› \N{ ZERO WIDTH NO-BREAK SPACE }:
\pC \p{Cf}
\p{All} \p{Any} \p{Assigned} \p{InArabicPresentationFormsB} \p{C} \p{Other} \p{Case_Ignorable} \p{CI} \p{Cf} \p{Format} \p{Changes_When_NFKC_Casefolded} \p{CWKCF} \p{Common} \p{Zyyy} \p{Default_Ignorable_Code_Point} \p{DI} \p{Graph} \p{Print}
\p{Age:1.1} \p{Bidi_Class:BN} \p{Bidi_Class=Boundary_Neutral} \p{Bidi_Class:Boundary_Neutral} \p{Bc=BN} \p{Block:Arabic_Presentation_Forms_B} \p{Canonical_Combining_Class:0} \p{Canonical_Combining_Class=Not_Reordered} \p{Canonical_Combining_Class:Not_Reordered} \p{Ccc=NR}
\p{Canonical_Combining_Class:NR} \p{Script=Common} \p{Decomposition_Type:None} \p{Dt=None} \p{East_Asian_Width=Neutral} \p{East_Asian_Width:Neutral} \p{Grapheme_Cluster_Break:CN} \p{Grapheme_Cluster_Break=Control} \p{Grapheme_Cluster_Break:Control} \p{GCB=CN}
\p{Hangul_Syllable_Type:NA} \p{Hangul_Syllable_Type=Not_Applicable} \p{Hangul_Syllable_Type:Not_Applicable} \p{Hst=NA} \p{Joining_Group:No_Joining_Group} \p{Jg=NoJoiningGroup} \p{Joining_Type:T} \p{Joining_Type=Transparent} \p{Joining_Type:Transparent} \p{Jt=T}
\p{Line_Break:WJ} \p{Line_Break=Word_Joiner} \p{Line_Break:Word_Joiner} \p{Lb=WJ} \p{Numeric_Type:None} \p{Nt=None} \p{Numeric_Value:NaN} \p{Nv=NaN} \p{Present_In:1.1} \p{Age=1.1} \p{In=1.1} \p{Present_In:2.0} \p{In=2.0} \p{Present_In:2.1} \p{In=2.1} \p{Present_In:3.0}
\p{In=3.0} \p{Present_In:3.1} \p{In=3.1} \p{Present_In:3.2} \p{In=3.2} \p{Present_In:4.0} \p{In=4.0} \p{Present_In:4.1} \p{In=4.1} \p{Present_In:5.0} \p{In=5.0} \p{Present_In:5.1} \p{In=5.1} \p{Present_In:5.2} \p{In=5.2} \p{Script:Common} \p{Sc=Zyyy} \p{Script:Zyyy}
\p{Sentence_Break:FO} \p{Sentence_Break=Format} \p{Sentence_Break:Format} \p{SB=FO} \p{Word_Break:FO} \p{Word_Break=Format} \p{Word_Break:Format} \p{WB=FO}
U+3000 ‹U+3000› \N{ IDEOGRAPHIC SPACE }:
\s \h \pZ \p{Zs}
\p{All} \p{Any} \p{Assigned} \p{Blank} \p{InCJKSymbolsAndPunctuation} \p{Changes_When_NFKC_Casefolded} \p{CWKCF} \p{Common} \p{Zyyy} \p{Z} \p{Zs} \p{Gr_Base} \p{Grapheme_Base} \p{GrBase} \p{HorizSpace} \p{Print} \p{Separator} \p{Space} \p{Space_Separator} \p{SpacePerl}
\p{White_Space} \p{WSpace}
\p{Age:1.1} \p{Bidi_Class:White_Space} \p{Bc=WS} \p{Bidi_Class:WS} \p{Bidi_Class=White_Space} \p{Block:CJK_Symbols_And_Punctuation} \p{Canonical_Combining_Class:0} \p{Canonical_Combining_Class=Not_Reordered} \p{Canonical_Combining_Class:Not_Reordered} \p{Ccc=NR}
\p{Canonical_Combining_Class:NR} \p{Script=Common} \p{Decomposition_Type:Non_Canon} \p{Decomposition_Type=Non_Canonical} \p{Decomposition_Type:Non_Canonical} \p{Dt=NonCanon} \p{Decomposition_Type:Wide} \p{Dt=Wide} \p{East_Asian_Width:F} \p{East_Asian_Width=Fullwidth}
\p{East_Asian_Width:Fullwidth} \p{Ea=F} \p{Grapheme_Cluster_Break:Other} \p{GCB=XX} \p{Grapheme_Cluster_Break:XX} \p{Grapheme_Cluster_Break=Other} \p{Hangul_Syllable_Type:NA} \p{Hangul_Syllable_Type=Not_Applicable} \p{Hangul_Syllable_Type:Not_Applicable} \p{Hst=NA}
\p{Joining_Group:No_Joining_Group} \p{Jg=NoJoiningGroup} \p{Joining_Type:Non_Joining} \p{Jt=U} \p{Joining_Type:U} \p{Joining_Type=Non_Joining} \p{Line_Break:ID} \p{Line_Break=Ideographic} \p{Line_Break:Ideographic} \p{Lb=ID} \p{Numeric_Type:None} \p{Nt=None}
\p{Numeric_Value:NaN} \p{Nv=NaN} \p{Present_In:1.1} \p{Age=1.1} \p{In=1.1} \p{Present_In:2.0} \p{In=2.0} \p{Present_In:2.1} \p{In=2.1} \p{Present_In:3.0} \p{In=3.0} \p{Present_In:3.1} \p{In=3.1} \p{Present_In:3.2} \p{In=3.2} \p{Present_In:4.0} \p{In=4.0} \p{Present_In:4.1}
\p{In=4.1} \p{Present_In:5.0} \p{In=5.0} \p{Present_In:5.1} \p{In=5.1} \p{Present_In:5.2} \p{In=5.2} \p{Script:Common} \p{Sc=Zyyy} \p{Script:Zyyy} \p{Sentence_Break:Sp} \p{SB=Sp} \p{Word_Break:Other} \p{WB=XX} \p{Word_Break:XX} \p{Word_Break=Other}
U+12345 ‹𒍅› \N{ CUNEIFORM SIGN URU TIMES KI }:
\w} \p{\pL} \p{L_} \p{Lo}
\p{All} \p{Any} \p{Alnum} \p{Alpha} \p{Alphabetic} \p{Assigned} \p{InCuneiform} \p{Cuneiform} \p{Is_Cuneiform} \p{Xsux} \p{L} \p{Lo} \p{Gr_Base} \p{Grapheme_Base} \p{Graph} \p{GrBase} \p{ID_Continue} \p{IDC} \p{ID_Start} \p{IDS} \p{Letter} \p{L_} \p{Other_Letter} \p{Print}
\p{Word} \p{XID_Continue} \p{XIDC} \p{XID_Start} \p{XIDS}
\p{Age:5.0} \p{Bidi_Class:L} \p{Bidi_Class=Left_To_Right} \p{Bidi_Class:Left_To_Right} \p{Bc=L} \p{Block:Cuneiform} \p{Canonical_Combining_Class:0} \p{Canonical_Combining_Class=Not_Reordered} \p{Canonical_Combining_Class:Not_Reordered} \p{Ccc=NR}
\p{Canonical_Combining_Class:NR} \p{Script=Cuneiform} \p{Block=Cuneiform} \p{Decomposition_Type:None} \p{Dt=None} \p{East_Asian_Width=Neutral} \p{East_Asian_Width:Neutral} \p{Grapheme_Cluster_Break:Other} \p{GCB=XX} \p{Grapheme_Cluster_Break:XX}
\p{Grapheme_Cluster_Break=Other} \p{Hangul_Syllable_Type:NA} \p{Hangul_Syllable_Type=Not_Applicable} \p{Hangul_Syllable_Type:Not_Applicable} \p{Hst=NA} \p{Joining_Group:No_Joining_Group} \p{Jg=NoJoiningGroup} \p{Joining_Type:Non_Joining} \p{Jt=U} \p{Joining_Type:U}
\p{Joining_Type=Non_Joining} \p{Line_Break:AL} \p{Line_Break=Alphabetic} \p{Line_Break:Alphabetic} \p{Lb=AL} \p{Numeric_Type:None} \p{Nt=None} \p{Numeric_Value:NaN} \p{Nv=NaN} \p{Present_In:5.0} \p{In=5.0} \p{Present_In:5.1} \p{In=5.1} \p{Present_In:5.2} \p{In=5.2}
\p{Script:Cuneiform} \p{Sc=Xsux} \p{Script:Xsux} \p{Sentence_Break:LE} \p{Sentence_Break=OLetter} \p{Sentence_Break:OLetter} \p{SB=LE} \p{Word_Break:ALetter} \p{WB=LE} \p{Word_Break:LE} \p{Word_Break=ALetter}
Ou, aller dans l'autre sens:
$ unichars '\pN' '\D' '\p{Latin}'
Ⅰ 8544 02160 ROMAN NUMERAL ONE
Ⅱ 8545 02161 ROMAN NUMERAL TWO
Ⅲ 8546 02162 ROMAN NUMERAL THREE
Ⅳ 8547 02163 ROMAN NUMERAL FOUR
Ⅴ 8548 02164 ROMAN NUMERAL FIVE
Ⅵ 8549 02165 ROMAN NUMERAL SIX
Ⅶ 8550 02166 ROMAN NUMERAL SEVEN
Ⅷ 8551 02167 ROMAN NUMERAL EIGHT
(etc)
$ unichars -a '\pL' '\p{Greek}' 'NFD ne NFKD' 'NAME =~ /SYMBOL/'
ϐ 976 3D0 GREEK BETA SYMBOL
ϑ 977 3D1 GREEK THETA SYMBOL
ϒ 978 3D2 GREEK UPSILON WITH HOOK SYMBOL
ϓ 979 3D3 GREEK UPSILON WITH ACUTE AND HOOK SYMBOL
ϔ 980 3D4 GREEK UPSILON WITH DIAERESIS AND HOOK SYMBOL
ϕ 981 3D5 GREEK PHI SYMBOL
ϖ 982 3D6 GREEK PI SYMBOL
ϰ 1008 3F0 GREEK KAPPA SYMBOL
ϱ 1009 3F1 GREEK RHO SYMBOL
ϲ 1010 3F2 GREEK LUNATE SIGMA SYMBOL
ϴ 1012 3F4 GREEK CAPITAL THETA SYMBOL
ϵ 1013 3F5 GREEK LUNATE EPSILON SYMBOL
Ϲ 1017 3F9 GREEK CAPITAL LUNATE SIGMA SYMBOL
Oh, et BNM « Brave New Millennium » , en se référant à notre monde moderne, post-ASCII dans lequel les caractères sont plus que sept bits dérisoires de large. ☺
String.replace () accepte la fonction de rappel en tant que second paramètre. (Je ne sais pas pourquoi tant de tutoriels JS omettent cette fonctionnalité utile.) Ainsi, nous pouvons écrire notre propre test des limites de mots.
La solution proposée ailleurs, avec regexp /(\W|^)(fancy namé|namé)(\W|$)/ig
, donne de faux positifs dans les cas de texte tels que « NAMEE ».
String.prototype.isWordCharAt = function(i) {
// should work for European languages and Unicode
return (this.charAt(i) >= 'A' && this.charAt(i) <= 'Z')
|| (this.charAt(i) >= 'a' && this.charAt(i) <= 'z')
|| (this.charCodeAt(i) >= 0xC0 && this.charCodeAt(i) < 0x2000)
;
};
"Namé. Goal: Some Fancy Namé. Namé. Nénamé. Namée. Nénamée. Namé"
.replace(/(Namé|Fancy Namé)/ig, function(
match, part1, /* part2, part3, ... */ offset, fullText) {
// Keep in mind that the number of arguments changes
// if the number of capturing parenthesis in regexp changes.
// We could use 'arguments' pseudo-array instead.
var len1 = part1.length;
var leftWordBoundary;
var rightWordBoundary;
if (offset === 0) {
leftWordBoundary = fullText.isWordCharAt(offset);
}
else {
leftWordBoundary = (fullText.isWordCharAt(offset - 1)
!= fullText.isWordCharAt(offset));
}
if (offset + len1 == fullText.length) {
rightWordBoundary = fullText.isWordCharAt(offset + len1 - 1);
}
else {
rightWordBoundary = (fullText.isWordCharAt(offset + len1 - 1)
!= fullText.isWordCharAt(offset + len1));
}
if (leftWordBoundary && rightWordBoundary) {
return '<a href="#">' + part1 + '</a>';
}
else {
return part1;
}
});
Peut-être essayer en utilisant les drapeaux de \o
ou \x
lorsque vous utilisez votre regex.
La fin de cette référence pour les expressions régulières Javascript pourrait aider vous sur.
Quant à ce que les valeurs réelles octal / hexagonaux é
est associé, je ne sais pas.