簡體   English   中英

具有多個條件的 JavaScript 中的正則表達式

[英]Regular Expression in JavaScript with multiple conditions

我想從我的 text_string 中刪除 javascript 中所有不是字母(在所有語言中)和數字的字符。 我可以單獨做。 但是如何將兩者都放在一個表達式中,以便兩個條件同時為真?

var text_string = '!#Ab+Z1_↕.🍏2ü翻訳';
text_string = text_string.replace(/\P{Letter}/gu, ''); 
text_string = text_string.replace(/\P{Number}/gu, ''); 
text_string = text_string.replace(/[^#]/, ''); 
// should be replaced to  #AbZ12ü翻訳

您可以在 unicode 中使用此正則表達式進行搜索:

[^\p{Letter}\p{Number}#]+

並用空字符串替換。

正則表達式演示

代碼:

 const regex = /[^\p{Letter}\p{Number}#]+/gu; // Alternative syntax using RegExp constructor // const regex = new RegExp('[^\\p{Letter}\\p{Number}#]+', 'gu') const str = `.#Ab+Z1_↕;2ü翻訳`. const result = str,replace(regex; ''). console;log(result);

正則表達式分解:

  • [^\p{Letter}\p{Number}#]+ :在字符 class 中匹配任何不是#不是 unicode 字母和不是 unicode 數字的字符。

請記住\p{something}\P{something} } 的倒數

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM