如何使用正则表达式在字符串中数字出现的开头添加\ n

时间:2019-02-26 07:37:29

标签: javascript regex split

我有一个包含巨大文本的对象,例如下面的示例。

var obj ={ "text" : "1This is the sample text. 2that I want to split. 3And add \n in the beginning of a number, 4whenever there is a number occurrence; in this string 4:1 for example i can have somewhere 5-6 also. How to achieve it 7Using javascript and 8regex"

我需要在出现该数字之前添加 \n <br>

我尝试了/([0-9])\w+/g,并按照以下方式加入了\n

请运行代码段以查看我的结果

var obj ={ "text" : "1This is the sample text. 2that I want to split. 3And add \n in the beginning of a number, 4whenever there is a number occurrence; in this string 4:1 for example i can have somewhere 5-6 also. How to achieve it 7Using javascript and 8regex"}

if(obj.text) {
    let quote = obj.text;
    var regex = /([0-9])\w+/g;
    var result = quote.split(regex).join('\n');
    console.log('result', result);
} 

我的预期输出:

  

1这是示例文本。

     

2我想拆分。

     

3在数字的开头加上\ n,

     

4每当有数字出现时;在这个字符串中

     

4:1例如我可以在某个地方

     

5-6也。如何实现

     

7使用javascript和

     

8regex

如何使用正则表达式和javascript实现它。请帮帮我!

先谢谢了。最好的答案将不胜感激。

4 个答案:

答案 0 :(得分:2)

您可以使用此正则表达式:

/(\d(?:[-:]\d)?)/g

并替换为

\n$1

代码:

var regex = /(\d(?:[-:]\d)?)/g;
var str = '1This is the sample text. 2that I want to split. 3And add \\n in the beginning of a number, 4whenever there is a number occurrence; in this string 4:1 for example i can have somewhere 5-6 also. How to achieve it 7Using javascript and 8regex';
var subst = '\n$1';

var result = str.replace(regex, subst);

console.log('result: ', result);

正则表达式也将匹配所有数字和一些非数字,因为显然您也想在4:55-6之前换行。正则表达式将匹配这些匹配项并将匹配的内容放入第1组。然后匹配项将替换为新行,然后再替换第1组中的任何内容。

答案 1 :(得分:1)

您可以使用

/\s([0-9])/g

replace ,所有在其前面带有空格\s并带有\n$1的数字:

$1是指捕获组([0-9])

var obj = {
  "text": "1This is the sample text. 2that I want to split. 3And add \n in the beginning of a number, 4whenever there is a number occurrence; in this string 4:1 for example i can have somewhere 5-6 also. How to achieve it 7Using javascript and 8regex"
}

if (obj.text) {
  let quote = obj.text;
  const result = quote.replace("\n", "\\n")
                      .replace(/\s([0-9])/g, '\n$1');
  console.log(result);
}

答案 2 :(得分:1)

您可以使用正则表达式在数字前插入换行符,然后在其后插入单词字符,连字符或冒号

quote.replace(/(?=\d+(?:[:-]|\w+))/g,'\n')

var obj ={ "text" : "1This is the sample text. 2that I want to split. 3And add \n in the beginning of a number, 4whenever there is a number occurrence; in this string 4:1 for example i can have somewhere 5-6 also. How to achieve it 7Using javascript and 8regex"}

if(obj.text) {
    let quote = obj.text;
    var result = quote.replace(/(?=\d+(?:[:-]|\w+))/g,'\n');
    console.log('Result: ', result);
} 

答案 3 :(得分:0)

您可以使用String.prototype.replace并将至少有一位数字\d+的任何出现替换为\n +刚替换的数字。如果您只想获取以单词或空格开头的数字,则可以使用(?:^|\s|\w)

后面的正数

var text = "1This is the sample text. 2that I want to split. 3And add \\n in the beginning of a number, 4whenever there is a number occurrence; in this string 4:1 for example i can have somewhere 5-6 also. How to achieve it 7Using javascript and 8regex";

var result = text.replace(/(?:^|\w|\s)(\d+)/g, "\n$1");
console.log(`result: ${result}`);