带有由多个字符组成的定界符的正则表达式(除了正常的 1 个字符的长定界符之外)
Regex with Delimiter consisting of multiple characters (in addition to normal, 1-character long delimiters)
我正在使用以下内容,在 .?!:;
处拆分一些文本并保留分隔符:
var str = 'Hello! Here is a file. It is called beatles.mp3. And so on!';
let arr = str.match(/[^\/.?!:;]+(?:[\/.?!:;]|$)/g);
// output ==> ["Hello!", "Here is a file.", "It is called beatles.", "mp3.", "And so on!"]
这很好,但我想有一种说法(而且,就像我现在所做的那样,保留分隔符):
“在有 .
的地方拆分,但如果有 .
后跟 mp3
,我希望你保留完整的 .mp3
。在其他任何地方,拆分那里有 .
"
想要的输出:
["Hello!", "Here is a file.", "It is called beatles.mp3.", "And so on!"]
您可以试试这个正则表达式:
const str = 'Hello! Here is a file. It is called beatles.mp3. And so on!';
const arr = str.match(/[^ ].+?(\.(?!mp3)|[\/?!:;])/g);
输出:
["Hello!", "Here is a file.", "It is called beatles.mp3.", "And so on!"]
你可以试试:
((?:\.(?!mp3)|[!&^?:;/]))
上面正则表达式的解释:
(?:\.(?!mp3)
- 表示不匹配 .
的非捕获组,如果它前面有 mp3
.
|
- 表示交替。
[!&^?:;/]
- 表示可能发生拆分的标点符号。您也可以添加其他标点符号。
\n
- 对于替换部分,使用捕获的组后跟换行符。最后拆分结果字符串并删除尾随出现的空格。
你可以在here.
中找到上述正则表达式的演示
/*
const regex = /(?:\. (?!mp3)|[!&^?:;/] ?)/g;
const str = `Hello! Here is a file. It is called beatles.mp3. And so on!`;
console.log(str.split(regex).filter(el => el));
*/
const regex = /((?:\.(?!mp3)|[!&^?:;/]))/gm;
const str = `Hello! Here is a file. It is called beatles.mp3. And so on!`;
const subst = `\n`;
// The substituted value will be contained in the result variable
const result = str.replace(regex, subst);
result.split("\n").forEach(el => console.log(el.trim()));
您可以匹配除分隔符之一以外的任何字符,包括不匹配点。
匹配点时,检查右边的是否不是mp3
。如果是这样,你可以匹配点。
重复该过程,直到遇到分隔符之一 .?!:;\/
([^.?!:;\/]+(?:\.(?=mp3\b)[^.?!:;\/]*)*[.?!:;\/]) ?
说明
(
捕获 组 1
[^.?!:;\/]+
匹配除列出的任何字符 1+ 次
(?:
非捕获组
\.(?=mp3\b)
匹配 .
并断言直接在右边的不是 mp3
[^.?!:;\/]*
匹配除列出的任何字符 0 次以上
)*
关闭非捕获组并重复0+次
[.?!:;\/]
匹配列出的其中一项
) ?
关闭组 1 并匹配一个可选的 space
该值在示例代码中的捕获组 1 m[1]
中。
const regex = /([^.?!:;\/]+(?:\.(?=mp3\b)[^.?!:;\/]*)*[.?!:;\/]) ?/g;
const str = `Hello! Here is a file. It is called beatles.mp3. And so on!`;
let m;
while ((m = regex.exec(str)) !== null) console.log(m[1]);
我正在使用以下内容,在 .?!:;
处拆分一些文本并保留分隔符:
var str = 'Hello! Here is a file. It is called beatles.mp3. And so on!';
let arr = str.match(/[^\/.?!:;]+(?:[\/.?!:;]|$)/g);
// output ==> ["Hello!", "Here is a file.", "It is called beatles.", "mp3.", "And so on!"]
这很好,但我想有一种说法(而且,就像我现在所做的那样,保留分隔符):
“在有 .
的地方拆分,但如果有 .
后跟 mp3
,我希望你保留完整的 .mp3
。在其他任何地方,拆分那里有 .
"
想要的输出:
["Hello!", "Here is a file.", "It is called beatles.mp3.", "And so on!"]
您可以试试这个正则表达式:
const str = 'Hello! Here is a file. It is called beatles.mp3. And so on!';
const arr = str.match(/[^ ].+?(\.(?!mp3)|[\/?!:;])/g);
输出:
["Hello!", "Here is a file.", "It is called beatles.mp3.", "And so on!"]
你可以试试:
((?:\.(?!mp3)|[!&^?:;/]))
上面正则表达式的解释:
(?:\.(?!mp3)
- 表示不匹配.
的非捕获组,如果它前面有mp3
.|
- 表示交替。[!&^?:;/]
- 表示可能发生拆分的标点符号。您也可以添加其他标点符号。\n
- 对于替换部分,使用捕获的组后跟换行符。最后拆分结果字符串并删除尾随出现的空格。
你可以在here.
中找到上述正则表达式的演示/*
const regex = /(?:\. (?!mp3)|[!&^?:;/] ?)/g;
const str = `Hello! Here is a file. It is called beatles.mp3. And so on!`;
console.log(str.split(regex).filter(el => el));
*/
const regex = /((?:\.(?!mp3)|[!&^?:;/]))/gm;
const str = `Hello! Here is a file. It is called beatles.mp3. And so on!`;
const subst = `\n`;
// The substituted value will be contained in the result variable
const result = str.replace(regex, subst);
result.split("\n").forEach(el => console.log(el.trim()));
您可以匹配除分隔符之一以外的任何字符,包括不匹配点。
匹配点时,检查右边的是否不是mp3
。如果是这样,你可以匹配点。
重复该过程,直到遇到分隔符之一 .?!:;\/
([^.?!:;\/]+(?:\.(?=mp3\b)[^.?!:;\/]*)*[.?!:;\/]) ?
说明
(
捕获 组 1[^.?!:;\/]+
匹配除列出的任何字符 1+ 次(?:
非捕获组\.(?=mp3\b)
匹配.
并断言直接在右边的不是mp3
[^.?!:;\/]*
匹配除列出的任何字符 0 次以上
)*
关闭非捕获组并重复0+次[.?!:;\/]
匹配列出的其中一项
) ?
关闭组 1 并匹配一个可选的 space
该值在示例代码中的捕获组 1 m[1]
中。
const regex = /([^.?!:;\/]+(?:\.(?=mp3\b)[^.?!:;\/]*)*[.?!:;\/]) ?/g;
const str = `Hello! Here is a file. It is called beatles.mp3. And so on!`;
let m;
while ((m = regex.exec(str)) !== null) console.log(m[1]);