求这个正则咋写

tangxu12 2018-11-18 02:17:24
我有一个字符串, 需要用 以<input 开始, >结束进行分割

例如字符串是 aaa <input /> bbb <input style='color:red'> ccc <input style='color:blue'> ddd

我想得到 aaa bbb ccc ddd数组

并且还要得到 每个input 里面 style的值(有的没有)

如何做?
...全文
51 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
tangxu12 2018-11-20
  • 打赏
  • 举报
回复
谢谢各位,结贴了.
文盲老顾 2018-11-19
  • 打赏
  • 举报
回复
"aaa <input /> bbb <input style='color:red'> ccc <input style='color:blue'> ddd".match(/[a-z0-9]+(?=[^>]*(<|$))|(?<=<input[^<>]*?style=\')([^\']+)/gi)
xuzuning 2018-11-18
  • 打赏
  • 举报
回复
s = "aaa <input />  bbb  <input style='color:red'>  ccc <input style='color:blue'> ddd";
e = /(?<=^|>)\s*(\w+)|(?<=style=')(.+?)(?=')/g;
r = s.match(e);
a=[];
b=[];
s.replace(e, function(m, n) { if(n == undefined) a.push(m); else b.push(m);});
document.write(a+'<br>'); //color:red,color:blue
document.write(b); //aaa, bbb, ccc, ddd
  • 打赏
  • 举报
回复
方法3:

var html=`aaa <input /> bbb <input style='color:red'> ccc <input style='color:blue'> ddd`;
html.split(/\s*(?:<input\b[^>]*?style='([^']*)'[^>]*?>|<input\s*\/?>)\s*/i).filter(function(item){
if(item) return item;
});
  • 打赏
  • 举报
回复
方法2:

var html=`aaa <input /> bbb <input style='color:red'> ccc <input style='color:blue'> ddd`;
var matchs=null;
var regex=/\w+(?=\s*<|\s*$)|style=(['"])(.*?)\1/ig;
var result=[];
while((matchs=regex.exec(html))!=null){
result.push(matchs[2]?matchs[2]:matchs[0]);
}
  • 打赏
  • 举报
回复
方法1:

var html=`aaa <input /> bbb <input style='color:red'> ccc <input style='color:blue'> ddd`;
var regex=/<input\b[^>]*?>/img;
html.replace(regex,function(m){
var item='';
if(m.search(/\bstyle\b/i)>-1){
item=m.match(/\bstyle=(['"])(.*?)\1/i)[2];
}
return item;
}).split(/\s+/);
天际的海浪 2018-11-18
  • 打赏
  • 举报
回复

var str = "aaa <input />  bbb  <input style='color:red'>  ccc <input style='color:blue'> ddd";
var arr = str.split(/\s*<input.*?>\s*/i);

87,910

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧