我正在研究一种最小标记语言,我试图允许用户使用/ 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?
有什么想法吗?
答案 0 :(得分:0)
你可以试试这个,想法是先匹配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);
答案 1 :(得分:0)
我认为我接近这种方式的方法是将所有不好的东西与一切好的东西相匹配。然后在表达式中只放你的正则表达式。稍后在编程逻辑中,我将测试每个匹配以查看是否填充了捕获组1,如果是,则match.index将显示匹配发生的字符串中的位置。
这个正则表达式将:
/
括号内的文字(如/match me/
)与捕获组1匹配 https?:\/\/[^\s]*|<\/?\w+\b(?=\s|>)(?:='[^']*'|="[^"]*"|=[^'"][^\s>]*|[^>])*>|(\/[^(\/|\<|\>)]*[^\/]*\/)
示例文字
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.
<强>匹配强>
[0] => Array
(
[0] => /slashes/
[1] => <b>
[2] => </b>
[3] => <b>
[4] => </b>
[5] => http://www.google.com/s]
[6] => /do/
[7] => <b>
[8] => /italic/
[9] => </b>
)
[1] => Array
(
[0] => /slashes/
[1] =>
[2] =>
[3] =>
[4] =>
[5] =>
[6] => /do/
[7] =>
[8] => /italic/
[9] =>
)