Mes*_*Qin 3 javascript regex svg match node.js
我有一个包含一个 SVG 路径的源:
<svg xmlns="http://www.w3.org/2000/svg" stroke="red" fill="grey"><path d="M 10 10 L 100 100 Q 200 50 300 100 A 80 50 20 1 0 400 600 Z" fill="none" stroke="red" stroke-width="5" /></svg>
Run Code Online (Sandbox Code Playgroud)
它也可以用单引号引起来
<svg xmlns="http://www.w3.org/2000/svg" stroke="red" fill="grey"><path d='M 10 10 L 100 100 Q 200 50 300 100 A 80 50 20 1 0 400 600 Z' fill="none" stroke="red" stroke-width="5" />
Run Code Online (Sandbox Code Playgroud)
我想要 <path=start quote 和 finish quote 之间的部分。
M 10 10 L 100 100 Q 200 50 300 100 A 80 50 20 1 0 400 600 Z
Run Code Online (Sandbox Code Playgroud)
我已经尝试过(JS)
var result = svg_tag.match(/<path(.*?)>/g).map(function(val){
return val.replace(/<path d="/g,'');
});
Run Code Online (Sandbox Code Playgroud)
但它返回
[
'M', '10',
'10', 'L',
'100', '100',
'Q', '200',
'50', '300',
'100', 'A',
'80', '50',
'20', '1',
'0', '400',
'600', 'Z"',
'fill="none"', 'stroke="red"',
'stroke-width="5"', '/>'
]
Run Code Online (Sandbox Code Playgroud)
[ \w]{10,}这RegExp匹配任何至少10包含字母、数字、空格和下划线的字符长的子字符串。
const regex = /[ \w]{10,}/;
const tag1 = '<svg xmlns="http://www.w3.org/2000/svg" stroke="red" fill="grey"><path d="M 10 10 L 100 100 Q 200 50 300 100 A 80 50 20 1 0 400 600 Z" fill="none" stroke="red" stroke-width="5" /></svg>';
const tag2 = `<svg xmlns="http://www.w3.org/2000/svg" stroke="red" fill="grey"><path d='M 10 10 L 100 100 Q 200 50 300 100 A 80 50 20 1 0 400 600 Z' fill="none" stroke="red" stroke-width="5" />`;
console.log(tag1.match(regex)[0]);
console.log(tag2.match(regex)[0]);Run Code Online (Sandbox Code Playgroud)