NodeJs – 从文本文件中删除多个数字和点号

huangapple go评论107阅读模式
英文:

NodeJs - Remove multiple numbers and dots from a text file

问题

我尝试使用Node.js删除数字和点号。

const replace = "1."
const replacer = new RegExp(replace, 'g', '')

function removeText() {
    let originalText = `
1. 我可以在这个地球上拥有无限的财富
2. 我的财富潜力无限
3. 我吸引无限的财务机会
    `;
    
    let newText = originalText.replace(replacer, '');

    console.log(newText);
}

removeText();

使用这段代码,我只能删除"1.",但是对于"2."、"3."等等呢?我需要处理1到100,有人能帮助我吗?

英文:

I tried to remove numbers along with dots using nodejs

const replace = "1."
const replacer = new RegExp(replace, 'g', '')

function removeText() {
    let originalText = '
1. I can own unlimited wealth on this earth
2. My potential for wealth knows no bounds
3. I attract limitless financial opportunities
    ';
    
    let newText = originalText.replace(replacer, '');

    console.log(newText);
}

removeText();

using this code I can only remove "1." but what about 2., 3., I have this counting upto 100, can someone help me?

答案1

得分: 2

你可以通过简单地使用数字 RegEx 来实现这个目标。

演示示例:

let originalText = `
1. I can own unlimited wealth on this earth
2. My potential for wealth knows no bounds
3. I attract limitless financial opportunities
`;

let newText = originalText.replace(/\d./g, '');

console.log(newText);
英文:

You can simply achieve this by just using digit RegEx.

Live Demo :

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

let originalText = `
1. I can own unlimited wealth on this earth
2. My potential for wealth knows no bounds
3. I attract limitless financial opportunities
`;

let newText = originalText.replace(/\d./g, &#39;&#39;);

console.log(newText);

<!-- end snippet -->

答案2

得分: 1

let originalText = `
1. I can own unlimited wealth on this earth
2. My potential for wealth knows no bounds
3. I attract limitless financial opportunities
    `;

originalText = originalText.split('\n').filter(line => line).map(line => line.split(/\d+\.\s/)[1]).join('\n');
英文:

split the string by lines. Filter out the empty values. Split each line by number followed by dot and finally join them back with new line.

let originalText = `
1. I can own unlimited wealth on this earth
2. My potential for wealth knows no bounds
3. I attract limitless financial opportunities
    `;

originalText=originalText.split(&#39;\n&#39;).filter(l=&gt;l).map(line=&gt;line.split(/\d+\.\s/)[1]).join(&#39;\n&#39;)

答案3

得分: 0

const text = `
1. 测试。
2. 测试。
3. 测试。
`;

const modifiedText = text.replace(/^\d+\. /gm, '');

console.log(modifiedText);

输出:

测试。
测试。
测试。
英文:

ChatGPT helped, To remove the line numbers (1., 2., and 3. Up to 100 and so on...) from the texts using only one function in Node.js, you can use the replace() function with a regular expression pattern. Here's an example:

const text = `
1. Test.
2. Test.
3. Test.
`;

const modifiedText = text.replace(/^\d+\. /gm, &#39;&#39;);

console.log(modifiedText);

Output:

Test.
Test.
Test.

In the above code, we use the replace() function with a regular expression ^\d+\. to match any line starting with one or more digits followed by a dot and a space. The ^ represents the start of the line, \d+ matches one or more digits, \. matches the dot character, and the space after it matches a space character. The g flag is used for a global search, and the m flag is used for multiline matching.

By replacing the matched pattern with an empty string, we effectively remove the line numbers from the text. Have a nice day 😊

huangapple
  • 本文由 发表于 2023年7月10日 20:58:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76653988.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定