简体   繁体   中英

Get part of string in javascript

How can I get coordinates from string:

<path fill="#FFFFFF" stroke="#000000" stroke-miterlimit="10" d="M275.843,110.301c-47.082,13.406-77.458,58.874-69.425,103.478
        c4.241,23.548,16.136,42.268,38.639,52.408c4.728,2.13,10.717,1.457,16.125,2.08c25.394,2.925,46.609,15.095,66.295,30.378
        c3.857,2.995,6.64,2.91,10.014-0.096c24.818-22.111,39.262-49.192,39.892-84.204c-0.189-2.728-0.244-6.743-0.775-10.694
        c-5.566-41.427-27.104-72.584-61.608-95.299c-1.563-1.029-3.687-1.618-5.573-1.682
        C298.075,106.287,286.912,107.148,275.843,110.301z"></path>

I need all coordinates after d=" to " , so I need only numbers:

M275.843,110.301c-47.082,13.406-77.458,58.874-69.425,103.478
            c4.241,23.548,16.136,42.268,38.639,52.408c4.728,2.13,10.717,1.457,16.125,2.08c25.394,2.925,46.609,15.095,66.295,30.378
            c3.857,2.995,6.64,2.91,10.014-0.096c24.818-22.111,39.262-49.192,39.892-84.204c-0.189-2.728-0.244-6.743-0.775-10.694
            c-5.566-41.427-27.104-72.584-61.608-95.299c-1.563-1.029-3.687-1.618-5.573-1.682
            C298.075,106.287,286.912,107.148,275.843,110.301z

or how can i get this value of d. Becouse target return me this all informations but when i write

target.d

there nothing happened

In vanilla javascript:

document.getElementById('elementId').getAttribute('d');

With jquery use .attr():

$('#elementId').attr('d');

Doing it with a Regexp :

var myString = '<path fill="#FFFFFF" stroke="#000000" stroke-miterlimit="10" d="M275.843,110.301c-47.082,13.406-77.458,58.874-69.425,103.478c4.241,23.548,16.136,42.268,38.639,52.408c4.728,2.13,10.717,1.457,16.125,2.08c25.394,2.925,46.609,15.095,66.295,30.378c3.857,2.995,6.64,2.91,10.014-0.096c24.818-22.111,39.262-49.192,39.892-84.204c-0.189-2.728-0.244-6.743-0.775-10.694c-5.566-41.427-27.104-72.584-61.608-95.299c-1.563-1.029-3.687-1.618-5.573-1.682C298.075,106.287,286.912,107.148,275.843,110.301z"></path>';

var myRegexp = /d=\"(.*|\s)\">/g;

var match = myRegexp.exec(myString);

What you want is then :

match[1]

使用getAttribute()方法:

event.target.getAttribute('d')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM