简体   繁体   English

如何使用JavaScript从字符串中删除特殊字符

[英]How to remove special characters from string using javascript

When # comes in string, I want to split it on new line using JavaScript. 当#出现在字符串中时,我想使用JavaScript在新行中拆分它。

Please help me. 请帮我。

Sample input: 输入样例:

This application helps the user to instantiate #Removed#Basic#afdaf#Clip#Python#matching of many parts#

Expected output: 预期产量:

This helps the user to instantiate 
Removed
Basic
afdaf
Clip
Python
matching of many parts

you can simply replace '#' by '\\n' 您可以简单地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')); 

Convert a string into array and loop through the array and print values one by one. 将字符串转换为数组,然后遍历数组,并逐个打印值。

 var str = "helps the user to instantiate #Removed#Basic#afdaf#Clip#Python#matching of many parts#"; str.split("#").forEach(function(entry) { console.log(entry); }); 

You can try this: 您可以尝试以下方法:

You should use the string replace function, with a single regex. 您应该使用带有单个正则表达式的字符串替换功能。 Assuming by special characters 假设有特殊字符

 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")); 

The below solution will split based on the # and store it in an array. 以下解决方案将基于#拆分并将其存储在数组中。 This solution will come in handy with splitting strings. 此解决方案可用于拆分字符串。

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.

相关问题 如何使用JavaScript从字符串中删除特殊字符 - How to remove the special characters from a string using javascript 如何使用Javascript从字符串中删除特殊字符 - How to remove special characters from a string using Javascript 使用 JavaScript 从字符串中删除除空格外的所有特殊字符 - Remove all special characters except space from a string using JavaScript 如何使用 JavaScript 中的正则表达式从字符串 (URL) 中删除 unicode 特殊字符 - How to remove unicode special characters from a string (URL) using regex in JavaScript 如何在Wordpress,PHP,JavaScript上使用正则表达式从字符串(URL)中删除特殊字符 - How to remove special characters from a string (URL) using regular expression on wordpress, php, javascript 如何从字符串中删除空格和特殊字符? - How to remove spaces and special characters from string? 从 JavaScript 中的字符串中删除除 @ 符号之外的所有特殊字符 - Remove all special characters except for @ symbol from string in JavaScript Javascript从字符串的开头和结尾删除特殊字符 - Javascript remove special characters from beginning and end of string 自定义 jquery 或 javascript 从字符串扩展名中删除特殊字符 - custom jquery or javascript remove special characters from a string extension JavaScript删除特殊字符字符串不起作用 - JavaScript Remove special characters string not working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM