英文:
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, '');
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('\n').filter(l=>l).map(line=>line.split(/\d+\.\s/)[1]).join('\n')
答案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, '');
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 😊
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论