374632897 / blog

前端小虾蟹的个人博客
7 stars 1 forks source link

正则表达式 #25

Open 374632897 opened 7 years ago

374632897 commented 7 years ago

获取一个 html元素的属性

const html = `<input type="text" class="form-control" style="width:248px;" value="点菜测试" data-reactid=".sceo9k633s.2.1.1.0.3.1.0.0">`;
html.match(/([\w]+)\s*?=\s*?(["'])([\s\S]*?)\2/g).map(item => item.slice().split('=').join(' => '))
// ["type => "text"", "class => "form-control"", "style => "width:248px;"", "value => "点菜测试"", "reactid => ".sceo9k633s.2.1.1.0.3.1.0.0""]