简体   繁体   English

jQuery在字符串中查找模式

[英]Jquery finding patterns in string

I need to find path information from a string using jquery and push the results into an array. 我需要使用jquery从字符串中查找路径信息,并将结果推送到数组中。

The string output looks like; 字符串输出看起来像;

 "var rsr = Raphael('rsr', '270', '266'); 
 var path_a = rsr.path('M144.869,199c0,7.659-5.479,13.139-13.14,
 13.139 c-7.659,0-14.598-5.479-14.598-13.139s6.209-13.139,13.869-
 13.139S144.869,191.341,144.869,199z'); path_a.attr({fill: 'non .... etc" 

I need to retrieve every occurrence of the path information with in rsr.path() ; 我需要使用rsr.path()来检索每次出现的路径信息;

How would i go about achieving this? 我将如何实现这一目标? using $.each on my string with a regex? 在正则表达式上在字符串上使用$ .each?

thanks cam 谢谢卡姆

JavaScript will let you do this with a regular expression: JavaScript将使您可以使用正则表达式执行此操作:

var s = " var rsr = Raphael('rsr', '270', '266'); var path_a = rsr.path('M144.869,199c0,7.659-5.479,13.139-13.14, 13.139 c-7.659,0-14.598-5.479-14.598-13.139s6.209-13.139,13.869- 13.139S144.869,191.341,144.869,199z'); path_a.attr({fill: 'non .... etc";

var regex = /M144\.869(.+?)869,199z/g;
var matches = regex.exec(s);

matches[0] then contains the capture group. 然后match [0]包含捕获组。

i found this worked.. 我发现这有效。

 $(document).ready(function(){

    $('form.svg-convert').submit(function(event){

        var content = $(this).find('textarea').val();
        process(content);

        event.preventDefault();
    })
})


var process = function(c){

    var content = c,
        paths = [];

    var contentSplit = content.split('path(');
    contentSplit.shift(); 

    $.each(contentSplit,function(i,v){

        var path = v.split(')');
        path = path[0];
        paths.push(path);

    });

} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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