0

当 # 出现在字符串中时,我想使用 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
4

4 回答 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'));

于 2019-02-06T05:52:11.850 回答
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);
    });

于 2019-02-06T05:53:14.617 回答
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"));

于 2019-02-06T05:49:26.293 回答
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(" "));
于 2019-02-06T05:51:44.430 回答