Skip to content

Instantly share code, notes, and snippets.

@nusendra
Last active December 12, 2019 19:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nusendra/21f006c23cd59ee0ddaffd41c2f86532 to your computer and use it in GitHub Desktop.
Save nusendra/21f006c23cd59ee0ddaffd41c2f86532 to your computer and use it in GitHub Desktop.
Extract normal letter and chinese character using regex
const mixedLetter: string = "what is this 肌肤困扰 means?";
/*
* Extract only the normal text (without chinese)
*/
const extractNormalText = (text: string): string => {
return text.replace(/[^a-z\d\s]+/gi, '');
}
/*
* Extract only the chinese characters
*/
const extractChineseCharacters = (text: string): string => {
return text.replace(/[^\u4E00-\u9FA5]+/gi, '');
};
console.log(extractNormalText(mixedLetter)); // "what is this means?"
console.log(extractChineseCharacters(mixedLetter)); // "肌肤困扰"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment