首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用regex在字符串中的数字开头添加\n

如何使用regex在字符串中的数字开头添加\n
EN

Stack Overflow用户
提问于 2019-02-26 07:37:29
回答 4查看 72关注 0票数 0

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

代码语言:javascript
复制
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,如下所示:

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

代码语言:javascript
复制
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我想分开。 在一个数字的开头加上\n, 每当有数字出现时;在这个字符串中 例如,我可以有个地方 也是5-6。如何实现 使用javascript和 8 8regex

如何使用regex和javascript实现它。请帮帮我!

提前谢谢。最好的答案将不胜感激。

EN

回答 4

Stack Overflow用户

发布于 2019-02-26 07:48:43

您可以使用这个regex:

代码语言:javascript
复制
/(\d(?:[-:]\d)?)/g

并代之以

代码语言:javascript
复制
\n$1

代码:

代码语言:javascript
复制
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组。然后,匹配将被替换为一个新的行,后面跟着第一组中的任何内容。

票数 2
EN

Stack Overflow用户

发布于 2019-02-26 07:48:45

你可以用

代码语言:javascript
复制
/\s([0-9])/g

replace所有在其前面有空格\s的数字都要用\n$1表示。

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

代码语言:javascript
复制
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);
}

票数 1
EN

Stack Overflow用户

发布于 2019-02-26 07:53:26

您可以使用“向前看”在数字前面插入一个换行符,后面跟着单词、字符、连字符或冒号,

代码语言:javascript
复制
quote.replace(/(?=\d+(?:[:-]|\w+))/g,'\n')

代码语言:javascript
复制
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);
} 

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54880498

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档