比如,我想在以下svg中:
代码: 全选
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- Created with Inkscape (http://www.inkscape.org/) -->
<svg
width="210mm"
height="297mm"
viewBox="0 0 210 297"
version="1.1"
id="svg5"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns="http://www.w3.org/2000/svg"
xmlns:svg="http://www.w3.org/2000/svg">
<defs
id="defs2">
<linearGradient
id="linearGradient20118">
<stop
style="stop-color:#aa4400;stop-opacity:1;"
offset="0"
id="stop20114" />
<stop
style="stop-color:#d0ab17;stop-opacity:0.99922938"
offset="1"
id="stop20116" />
</linearGradient>
<rect
x="98.353481"
y="349.7599"
width="623.66439"
height="453.02314"
id="rect2893" />
<linearGradient
xlink:href="#linearGradient20118"
id="linearGradient20082"
x1="171.27736"
y1="435.22574"
x2="174.11577"
y2="358.83124"
gradientUnits="userSpaceOnUse" />
<linearGradient
xlink:href="#linearGradient20118"
id="linearGradient20776"
gradientUnits="userSpaceOnUse"
x1="171.27736"
y1="435.22574"
x2="174.11577"
y2="358.83124" />
<linearGradient
xlink:href="#linearGradient20118"
id="linearGradient20778"
gradientUnits="userSpaceOnUse"
x1="171.27736"
y1="435.22574"
x2="174.11577"
y2="358.83124" />
</defs>
<g
id="layer1">
<g
aria-label="尹卂"
transform="matrix(0.26458333,0,0,0.26458333,11.317413,-24.19429)"
id="g20864"
style="font-size:72px;line-height:1.25;white-space:pre;shape-inside:url(#rect2893);fill:#aa4400;fill-opacity:1">
<path
d="m 108.43352,365.81577 v 5.184 h 19.512 v 12.312 h -25.92 v 5.256 h 25.92 v 1.872 c 0,3.528 -0.144,6.984 -0.864,10.296 h -19.44 v 5.184 h 17.856 c -2.88,6.984 -9,13.32 -21.888,18.072 1.224,0.936 3.024,2.952 3.816,4.176 14.76,-5.616 21.24,-13.464 23.976,-22.248 h 22.104 v 3.744 h 5.472 v -21.096 h 7.992 v -5.256 h -7.992 v -17.496 z m 24.912,22.752 h 20.16 v 12.168 h -20.88 c 0.576,-3.384 0.72,-6.84 0.72,-10.296 z m 0,-5.256 v -12.312 h 20.16 v 12.312 z"
style="-inkscape-font-specification:sans-serif;fill:#aa4400;fill-opacity:1"
id="path20860" />
<path
d="m 175.75338,365.59977 v 5.04 h 46.728 c 0.36,31.032 1.44,57.384 11.736,57.384 4.248,0 5.616,-4.032 6.12,-15.336 -1.152,-0.648 -2.664,-2.088 -3.744,-3.456 -0.144,7.92 -0.648,12.888 -1.872,12.888 -5.904,0 -6.912,-29.592 -6.84,-56.52 z m 19.8,8.064 v 18.36 h -19.368 v 5.04 h 19.368 v 30.744 h 5.4 v -30.744 h 19.152 v -5.04 h -19.152 v -18.36 z"
style="-inkscape-font-specification:sans-serif;fill:#aa4400;fill-opacity:1"
id="path20862" />
</g>
<g
aria-label="尹卂"
transform="matrix(0.26458333,0,0,0.26458333,6.6675677,-28.513204)"
id="text2891"
style="font-size:72px;line-height:1.25;white-space:pre;shape-inside:url(#rect2893);fill:url(#linearGradient20082);fill-opacity:1">
<path
d="m 108.43352,365.81577 v 5.184 h 19.512 v 12.312 h -25.92 v 5.256 h 25.92 v 1.872 c 0,3.528 -0.144,6.984 -0.864,10.296 h -19.44 v 5.184 h 17.856 c -2.88,6.984 -9,13.32 -21.888,18.072 1.224,0.936 3.024,2.952 3.816,4.176 14.76,-5.616 21.24,-13.464 23.976,-22.248 h 22.104 v 3.744 h 5.472 v -21.096 h 7.992 v -5.256 h -7.992 v -17.496 z m 24.912,22.752 h 20.16 v 12.168 h -20.88 c 0.576,-3.384 0.72,-6.84 0.72,-10.296 z m 0,-5.256 v -12.312 h 20.16 v 12.312 z"
style="-inkscape-font-specification:sans-serif;fill:url(#linearGradient20776);fill-opacity:1"
id="path15631" />
<path
d="m 175.75338,365.59977 v 5.04 h 46.728 c 0.36,31.032 1.44,57.384 11.736,57.384 4.248,0 5.616,-4.032 6.12,-15.336 -1.152,-0.648 -2.664,-2.088 -3.744,-3.456 -0.144,7.92 -0.648,12.888 -1.872,12.888 -5.904,0 -6.912,-29.592 -6.84,-56.52 z m 19.8,8.064 v 18.36 h -19.368 v 5.04 h 19.368 v 30.744 h 5.4 v -30.744 h 19.152 v -5.04 h -19.152 v -18.36 z"
style="-inkscape-font-specification:sans-serif;fill:url(#linearGradient20778);fill-opacity:1"
id="path15633" />
</g>
</g>
</svg>
正則表達式開啟m並沒有用,因為.不包含\n。
我的解決辦法是,用(.|\n)*?來匹配包括換行在內的所有字符。即:
代码: 全选
if (svgtxt.match(/<g(.|\n)*?(<g(.|\n)*?transform(.|\n)*?<\/g>)(.|\n)*?(<g(.|\n)*?transform(.|\n)*?<\/g>)/)) {