javascript - 正则表达式 - 选择不是标签名称或属性的单词

标签 javascript regex

是否可以选择所有非标签且不在标签内的单词作为属性?我已经得到了这个逆工作,我知道我可以分两个阶段完成这个工作,替换第一个匹配项并进行新的 Javascript RegExp 搜索。但问题是我想让它用一种表达方式工作。

http://regexr.com/3cb6g

(<[^>]*>)|({[^>]*})

输入:

<p>Test image captions for GitBook:</p>

<p>Second image: <img scr="./image2.png" alt="image title" title="image title">asdf</img>{caption width="300" style="height:'300px'"} </p>

<p>Sample text and first image: <img scr="./image1.png" alt="image 1" /> {caption width="300" style="height:'300px'"} for testing ok...</p>

预期输出标记`内应匹配的单词:

<p>`Test` `image` `captions` `for` `GitBook`:</p>

<p>`Second` `image`: <img scr="./image2.png" alt="image title" title="image title">`asdf`</img>{caption width="300" style="height:'300px'"} </p>

<p>`Sample` `text` `and` `first` `image`: <img scr="./image1.png" alt="image 1" /> {caption width="300" style="height:'300px'"} `for` `testing` `ok`...</p>

最佳答案

我的问题可能不太清楚,因为答案是使用 JavaScript 代码来处理匹配。我的目的是仅用简单的表达来找到解决方案。我终于找到了这个满足我需求的表达方式:

((?!([^<]+)?>)([\w]+)(?!([^\{]+)?\})([\w]+))

http://regexr.com/3cb6j

关于javascript - 正则表达式 - 选择不是标签名称或属性的单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34059249/

相关文章:

javascript - jQuery 中的上下文菜单项

带有 unicode 和标点符号的 Javascript 正则表达式

javascript - 解释表单附加到 DOM 的对象

html - 从大型文本/HTML 文件中提取 URL

python - 将点分隔的字符串拆分为单词,但有一个特殊情况

javascript - jquery 驼峰式实现

javascript - 如何在提交表单后保留选定的日期?

php - 从php中的字符串替换br标签

php - 在 PHP 中用一个空格替换多个(仅内部)空格?

javascript - RegExp 只允许单词之间有一个空格