[英]How to remove special characters from string using javascript
当#出现在字符串中时,我想使用JavaScript在新行中拆分它。
请帮我。
输入样例:
This application helps the user to instantiate #Removed#Basic#afdaf#Clip#Python#matching of many parts#
预期产量:
This helps the user to instantiate
Removed
Basic
afdaf
Clip
Python
matching of many parts
您可以简单地replace
'#'
replace
为'\\n'
var mainVar = 'This application helps the user to instantiate#Removed#Basic#afdaf#Clip#Python#matching'; console.log(mainVar.replace(/[^\\w\\s]/gi, '\\n'));
将字符串转换为数组,然后遍历数组,并逐个打印值。
var str = "helps the user to instantiate #Removed#Basic#afdaf#Clip#Python#matching of many parts#"; str.split("#").forEach(function(entry) { console.log(entry); });
您可以尝试以下方法:
您应该使用带有单个正则表达式的字符串替换功能。 假设有特殊字符
var str = "This application helps the user to instantiate #Removed#Basic#afdaf#Clip#Python#matching of many parts#"; console.log(str.replace(/[^a-zA-Z ]/g, "\\n"));
以下解决方案将基于#拆分并将其存储在数组中。 此解决方案可用于拆分字符串。
var sentence = '#Removed#Basic#afdaf#Clip#Python#matching of many parts#'
var newSentence = [];
for(var char of sentence.split("#")){
console.log(char); // This will print each string on a new line
newSentence.push(char);
}
console.log(newSentence.join(" "));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.