Home > Web Front-end > JS Tutorial > body text

js 替换_javascript技巧

WBOY
Release: 2016-05-16 19:06:04
Original
1143 people have browsed it

问题:就是将()()()((())())换成[][][[[]][]]的那种了,处理括弧配对用的
作者:infinte
要求:
[1]支持任意的“括弧”,也就是可以用【】{}()……或者类似XML的:、[cc][cc:over]、{ttt] [ttt}
[2]严格按照层次匹配,就是(a)b(c(d)e)换成[a]b[c[d]e]而非[a]b[c(d]e) (结束太早了)
[3]左括号比右括号多时,保留多余的括号,即:()()((()()) → [][]([[][]]
[4]右括号比左括号多,保留多余的括号,即:(()()(()()))))) → [[][][[][]]])))

方案一:使用堆栈
作者:winter
代码:

复制代码 代码如下:

 

方案二:使用正则
作者:月影
代码:
复制代码 代码如下:

 
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!