【发布时间】:2022-08-19 19:57:22
【问题描述】:
标签: javascript regex
标签: javascript regex
此正则表达式将针对最大长度为 25 的句子。
/(?<=^|\.)\s*.{1,25}?\./gms
测试 sn-p :
const regex = /(?<=^|\.)\s*.{1,25}?\./gms;
const str = `This is a test. Keep this longer text that has over 25 characters. Remove this small text. `;
const result = str.replace(regex, '');
console.log(result);
或者没有后视。对于落后的浏览器。
/(^|\.)\s*.{1,25}?\./gms
替换为第一个捕获组。
const regex = /(^|\.)\s*.{1,25}?\./gms;
const str = `This is a test. Keep this longer text that has over 25 characters.
Remove this small text. `;
const result = str.replace(regex, '$1');
console.log(result);
【讨论】:
/(^|\.)\s*.{1,25}?(?=\.)/gms。
也许这个有帮助。我没有考虑'。字符 因为我在 JS 中填充了这句话。
const sentence = (() => {
const sentences = [];
for (let i = 0; i < 15; i++) {
const len = Math.floor(Math.random() * (30 - 15 + 1) + 15);
const sentence = [];
for (let j = 0; j < len; j++) {
sentence.push(String.fromCharCode(Math.floor(Math.random() * (122 - 97 + 1) + 97)));
}
sentences.push(sentence.join(''));
}
return sentences
})();
console.log(sentence.length)
console.log(sentence)
console.log(sentence.filter(s => s.length > 24))
console.log(sentence.filter(s => s.length > 24).length)
【讨论】: