Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
931 views
in Technique[技术] by (71.8m points)

regex - How to replace non-printable unicode characters (Javascript)

I've already wasted a good amount of time dealing with strings (generated by some other source) and I found out that the problem was that the strings have non-printable characters. Today I am dealing with javascript. Does anyone know how to replace non-printable unicode characters in javascript?

I found something similar here:

How can I replace non-printable Unicode characters in Java?

my_string.replaceAll("\p{C}", "?");

and here:

Non-ascii characters added form input only with Safari Browser

filename.replace(/[^a-z0-9.]+/gi, "");

The last option replaces all the characters that are not in the brackets. This is something that has always comes to bite me in my rear end no matter what language I'm working on and I'm tired of trying to figure out what characters are messing up my code. For this reason I want to be able to replace all those invisible characters for something visible so that I can later remove them completely.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Based on what p{C} is in java (from XRegExp):

var re = /[-x1Fx7F-x9FxADu0378u0379u037F-u0383u038Bu038Du03A2u0528-u0530u0557u0558u0560u0588u058B-u058Eu0590u05C8-u05CFu05EB-u05EFu05F5-u0605u061Cu061Du06DDu070Eu070Fu074Bu074Cu07B2-u07BFu07FB-u07FFu082Eu082Fu083Fu085Cu085Du085F-u089Fu08A1u08AD-u08E3u08FFu0978u0980u0984u098Du098Eu0991u0992u09A9u09B1u09B3-u09B5u09BAu09BBu09C5u09C6u09C9u09CAu09CF-u09D6u09D8-u09DBu09DEu09E4u09E5u09FC-u0A00u0A04u0A0B-u0A0Eu0A11u0A12u0A29u0A31u0A34u0A37u0A3Au0A3Bu0A3Du0A43-u0A46u0A49u0A4Au0A4E-u0A50u0A52-u0A58u0A5Du0A5F-u0A65u0A76-u0A80u0A84u0A8Eu0A92u0AA9u0AB1u0AB4u0ABAu0ABBu0AC6u0ACAu0ACEu0ACFu0AD1-u0ADFu0AE4u0AE5u0AF2-u0B00u0B04u0B0Du0B0Eu0B11u0B12u0B29u0B31u0B34u0B3Au0B3Bu0B45u0B46u0B49u0B4Au0B4E-u0B55u0B58-u0B5Bu0B5Eu0B64u0B65u0B78-u0B81u0B84u0B8B-u0B8Du0B91u0B96-u0B98u0B9Bu0B9Du0BA0-u0BA2u0BA5-u0BA7u0BAB-u0BADu0BBA-u0BBDu0BC3-u0BC5u0BC9u0BCEu0BCFu0BD1-u0BD6u0BD8-u0BE5u0BFB-u0C00u0C04u0C0Du0C11u0C29u0C34u0C3A-u0C3Cu0C45u0C49u0C4E-u0C54u0C57u0C5A-u0C5Fu0C64u0C65u0C70-u0C77u0C80u0C81u0C84u0C8Du0C91u0CA9u0CB4u0CBAu0CBBu0CC5u0CC9u0CCE-u0CD4u0CD7-u0CDDu0CDFu0CE4u0CE5u0CF0u0CF3-u0D01u0D04u0D0Du0D11u0D3Bu0D3Cu0D45u0D49u0D4F-u0D56u0D58-u0D5Fu0D64u0D65u0D76-u0D78u0D80u0D81u0D84u0D97-u0D99u0DB2u0DBCu0DBEu0DBFu0DC7-u0DC9u0DCB-u0DCEu0DD5u0DD7u0DE0-u0DF1u0DF5-u0E00u0E3B-u0E3Eu0E5C-u0E80u0E83u0E85u0E86u0E89u0E8Bu0E8Cu0E8E-u0E93u0E98u0EA0u0EA4u0EA6u0EA8u0EA9u0EACu0EBAu0EBEu0EBFu0EC5u0EC7u0ECEu0ECFu0EDAu0EDBu0EE0-u0EFFu0F48u0F6D-u0F70u0F98u0FBDu0FCDu0FDB-u0FFFu10C6u10C8-u10CCu10CEu10CFu1249u124Eu124Fu1257u1259u125Eu125Fu1289u128Eu128Fu12B1u12B6u12B7u12BFu12C1u12C6u12C7u12D7u1311u1316u1317u135Bu135Cu137D-u137Fu139A-u139Fu13F5-u13FFu169D-u169Fu16F1-u16FFu170Du1715-u171Fu1737-u173Fu1754-u175Fu176Du1771u1774-u177Fu17DEu17DFu17EA-u17EFu17FA-u17FFu180Fu181A-u181Fu1878-u187Fu18AB-u18AFu18F6-u18FFu191D-u191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7-uD7CAuD7FC-uF8FFuFA6EuFA6FuFADA-uFAFFuFB07-uFB12uFB18-uFB1CuFB37uFB3DuFB3FuFB42uFB45uFBC2-uFBD2uFD40-uFD4FuFD90uFD91uFDC8-uFDEFuFDFEuFDFFuFE1A-uFE1FuFE27-uFE2FuFE53uFE67uFE6C-uFE6FuFE75uFEFD-uFF00uFFBF-uFFC1uFFC8uFFC9uFFD0uFFD1uFFD8uFFD9uFFDD-uFFDFuFFE7uFFEF-uFFFBuFFFEuFFFF]/g;
filename.replace(re, "");

You could alternatively just use the library


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...