1
66beta 2021-12-30 16:08:40 +08:00
给我整不会了,代码贴一下吧
|
2
dethan 2021-12-30 16:32:40 +08:00 via Android
会写这么长的表达式吗?
|
3
shubei OP @66beta 不用代码呀,就是正则表达式可以$0 表示匹配到的部分, $n 表示用()包裹的 part ,实际使用发现只能用到$99
|
5
66beta 2021-12-30 16:43:31 +08:00
说说你要干啥吧,不要纠结语言本身的功能
|
6
jifengg 2021-12-30 16:46:25 +08:00 1
https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/String/replace#%E4%BD%BF%E7%94%A8%E5%AD%97%E7%AC%A6%E4%B8%B2%E4%BD%9C%E4%B8%BA%E5%8F%82%E6%95%B0
MDN 文档说“n 是个小于 100 的非负整数”,如果支持命名分组捕获,可以使用$<Name> |
7
shubei OP @66beta 大概就是在一个字符串里面搜索:
比如在 <\span>高<\/span> <\span class="del">情<\/span> <\span class="del">已<\/span> <\span class="title"><\span class="add">逐<\/span><\span class="add">晓<\/span><\/span> <\span>云</span> <\span>空</span> 搜索’已逐‘ 我目前就是根据搜索词动态生成正则:已(<span[^>]*>|</span>)*逐(<span[^>]*>|</span>)* 替换成 <span class='match'>已</span>$1<span class='match'>逐</span>$2 |
9
66beta 2021-12-30 17:15:03 +08:00
'<\span>高<\/span><\span class="del">情<\/span><\span class="del">已<\/span><\span class="title"><\span class="add">逐<\/span><\span class="add">晓<\/span><\/span><\span>云</span><\span>空</span>'.replace(/<[^<\/]*>(?=[已|逐])/gmi, '<\span class="match">')
=> '<span>高</span><span class="del">情</span><span class="match">已</span><span class="title"><span class="match">逐</span><span class="add">晓</span></span><span>云</span><span>空</span>' |
10
shubei OP |
11
lululau 2021-12-30 18:47:09 +08:00
s.replace(REGEX, (_, ...g) => g[123]))
|
12
IvanLi127 2021-12-31 12:56:04 +08:00 via Android
设计者可能死都想不通你咋能用得了那么多😂
你这样用性能没问题吗😂 |