如何从字符串使用javascript除去特殊字符

问题描述 投票:0回答:4

当#进来的字符串,我想将它拆分上使用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
javascript
4个回答
1
投票

你可以简单地通过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'));

1
投票

将一个字符串转换阵列和循环通过阵列和打印值一个接一个。

var str = "helps the user to instantiate #Removed#Basic#afdaf#Clip#Python#matching of many parts#";

    str.split("#").forEach(function(entry) {
        console.log(entry);
    });

0
投票

你可以试试这个:

你应该使用字符串替换功能,用一个单一的正则表达式。通过特殊字符假设

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

0
投票

下面的解决方案将基于所述分割#并将其存储在数组中。该解决方案会派上用场,以极快的字符串。

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(" "));
© www.soinside.com 2019 - 2024. All rights reserved.