gpt4 book ai didi

正则表达式:查找不在某些标签内的对

转载 作者:行者123 更新时间:2023-12-01 08:17:22 25 4
gpt4 key购买 nike

我正在研究一种极简标记语言,我正在尝试允许用户使用/slashes/来显示斜体文本。但是,我似乎无法获得正常工作的正则表达式。

RegexPal 链接是 here .

这是我目前所拥有的,但还不太奏效:

\/[^(\/|\<|\>)]*[^\/]*\/

我正在测试的值:

I am attempting to replace values in /slashes/ with italic tags.  The challenge: the mark up allows URLs to be placed into [http://www.google.com/s] square bracket tags, messing things up further.  Now the tags are off balanced.  What /do/ I do?  I'd ideally like to have it skip searching [] tags?

有什么想法吗?

最佳答案

你可以试试这个,思路是匹配之前括号内的html标签或内容,自己替换。

var str = "I am attempting to replace values in /slashes/ with italic tags.  One problem is HTML: If I do <b>html</b> <b>tags</b> tags, it picks up the closures.  Also, the mark up allows URLs to be placed into [http://www.google.com/s] square bracket tags, messing things up further.  Now the tags are off balanced.  What /do/ I do?  I'd ideally like to have it skip searching [] and inside <> tags.  Doing <b>/italic/</b> should be legal, however.";

str = str.replace(/<[^>]+>|\[[^\]]+\]|\/([^\/]*)\//g, function (m, p1) {
return (p1) ? '<i>' + p1 + '</i>' : m; });

console.log(str);

关于正则表达式:查找不在某些标签内的对,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17371328/

25 4 0