【发布时间】:2018-12-07 08:18:22
【问题描述】:
我有一个大字符串,我需要删除短语“Pages 1 of 3 Printed on:”,我正在尝试使用正则表达式使 X 的 1 变为动态。我也尝试过使用通配符和字符串模板,但我没有运气。
我目前正在对其进行硬编码。例如,如果我硬编码 3,它会找到整个短语,将其替换为空,并找到任何其他实例。任何帮助表示赞赏!
let text = 'Pages 1 of 3 Printed on: FIRST NAME Pages 2 of 3 Printed on: TITLE SIGNATURE Pages 3 of 3 Printed on: STAN SMITH'
// Remove Pages
if (text('Pages 1 of ' + /[0-9]/ + 'Printed on: ')) {
// Removing Begins
console.log('Removing: Pages 1 of ' + /[0-9]/ + 'Printed on: ');
// Remove Text
text = text('Pages 1 of ' + /[0-9]/ + 'Printed on: ', '');
// Find Any Other Instances Of Text
while (text.includes('Pages 1 of /[1-9]/gPrinted on: ', '')) {
text = text('Pages 1 of ' + /[0-9]/ + 'Printed on: ', '', '');
}
// Removing Ends
console.log('Removed: Pages 1 of ' + /[0-9]/ + 'Printed on: ');
}
【问题讨论】:
标签: javascript regex string