Created
September 22, 2013 08:01
-
-
Save smallnewer/6657798 to your computer and use it in GitHub Desktop.
以特殊符号分割指定字符串。具体描述见评论
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!doctype html> | |
| <html lang="en"> | |
| <head> | |
| <meta charset="UTF-8"> | |
| <title>Document</title> | |
| </head> | |
| <body> | |
| <div>\||di\|v|ab\@ca@大点的a:吧啊</div> | |
| <script> | |
| var str = document.querySelector("div").innerHTML; | |
| // alert(str) | |
| var reg = /((?:.*?(?:\\[\|\:\@])*)[^\\]*?)([\|\:\@]|[^\|\:\@]$)/gi; | |
| var ret = []; | |
| var length = str.length; | |
| str.replace(reg,function ($,$1,$2,ind) { | |
| console.log(arguments); | |
| if (ind + $.length < length) { | |
| $ = $.slice(0,-1); | |
| }; | |
| ret.push($); | |
| }) | |
| console.log(ret); | |
| </script> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
在做MVVM库时,有这样一个场景,在标签上书写指令需要用|或者:分割开。
譬如
<div mv-attr="{{abc}}|filter_fn:otherthing"></div>。在这个场景中符号是确定的,比如|后的
filter_fn就是一个过滤函数,而:后的otherthing可能用于做其他用处。但将来可能需要添加@等其他符号,用于支持更多的指令。所以需要一段代码,在上述场景中,能按照指定符号(比如|:@&$等等)分割字符串,得到指令的各个部分,并知道其分隔符是什么。以便库可以进行相应的处理。
这段代码的主要作用就是于此。