Last active
December 12, 2019 19:48
-
-
Save nusendra/21f006c23cd59ee0ddaffd41c2f86532 to your computer and use it in GitHub Desktop.
Extract normal letter and chinese character using regex
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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