[英]Replace special characters in a string with _ (underscore)
我想從字符串中刪除特殊字符並用_
字符替換它們。
例如:
string = "img_realtime_tr~ading3$"
結果字符串應類似於“img_realtime_tr_ading3_”;
我需要替換這些字符: & / \\ # , + ( ) $ ~ % .. ' " : * ? < > { }
string = string.replace(/[&\/\\#,+()$~%.'":*?<>{}]/g,'_');
或者,要更改除數字和字母以外的所有字符,請嘗試:
string = string.replace(/[^a-zA-Z0-9]/g,'_');
string = string.replace(/[\W_]/g, "_");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.