匹配引号内的换行符
Match newline characters inside quotes
本文关键字:换行符 更新时间:2023-09-26
我正在寻找一种解决方案来替换Javascript中XML属性(XML字符串)中的换行符。(试图用
替换,这样在解析XML时就不会丢失换行符)
注意:字符串中还有其他换行符,例如<main> .. </body>
之后
(对任何其他正则表达式/非正则表达式解决方案开放!)
<main>
<head/><body><note text="Load">
<note text="2 Newlines at the end of this
newline"/>
</body>
</main>
我只有这个可怕的东西"['s'S]*?(['r'n]+)['s'S]*?"
https://regex101.com/r/vE2lD7/2
下面是一个进行转换的小片段:
function convert(xmlStr) {
var xml = document.createElement('xml');
xml.innerHTML = xmlStr;
[].forEach.call(xml.querySelectorAll('note[text]'), function (note) {
note.setAttribute('text',
note.getAttribute('text').replace(/'r?'n/g,'@#10'));
});
return xml.innerHTML.replace(/@#10/g, '
');
}
// I/O
var button = document.querySelector('button');
var input = document.querySelector('textarea');
var output = document.querySelector('pre');
// click handler
button.onclick = function () {
output.textContent = convert(input.value);
}
textarea { width: 25em; height: 10em; float:left}
<textarea><main>
<head/><body><note text="Load">
<note text="2 Newlines at the end of this
newline"/>
</body>
</main></textarea>
<button>convert</button>
<pre></pre>
请注意,立即替换为

将导致&
被转义为&
。
https://regex101.com/r/vE2lD7/3
您希望将正则表达式更改为不贪婪。这里有一篇很好的文章描述它:
https://docs.oracle.com/javase/tutorial/essential/regex/quant.html
相关文章:
- 如何在映射数组中添加换行符
- JQuery:向多个匹配结果添加换行符的最简单方法
- 在文本区域中使用jQuery.text()保持换行符
- javascript替换换行符和特殊字符
- 换行符拆分返回“”;未定义”;
- 如何使用web浏览器在NoSQL数据中呈现换行符
- 换行符插入jquery选择器
- jquery使用val()复制值似乎可以删除换行符
- 文本区域-获取每一行,找到换行符
- 处理字段中带有换行符的csv文件-node.js
- 使用jquery将单个换行符替换为span元素
- javascript/jQuery:在title中添加换行符——在IE9中有效,但在IE8中无效
- 咖啡脚本意外换行符
- 在文本区域中保留换行符.value
- 捕捉文本中的换行符
- JavaScript字符串中的换行符也保存为.txt
- 尊重“; 〃;从HTML显示中的API响应返回的换行符(和特殊字符)
- 将换行符写入<span>元素
- 根据换行符选择文本
- 使用PHP输出JavaScript时会出现神秘的换行符