簡體   English   中英

使用RegEx發出分割字符串,包括括號,分號和空格

[英]Issue splitting string with RegEx that includes brackets, semicolons and spaces

我在下面有一個字符串:

var sphere1 = CSG.sphere({center: [5, 5, 5], radius: 10, resolution: resolution });<br>
var sphere2 = sphere1.translate([12, 5, 0]);<br>

我嘗試使用以下(/,| |[|]|;/作為正則表達式)進行拆分:

var words = $(selector).html().split(/,| |[|]|;/);

我有三個問題:

  • 我在行尾(在BR標簽之前)丟失了分號-應該將它們在WORDS數組中獨立分割

  • 括號中包含第一個數字的字符串(5或12)也包含開括號

  • 括號中最后一個數字(5或0)的字符串也包括右括號。

基本上,我試圖專門“隔離”任何數字,以便我可以分別遞增/修改它們。

對RegEx有什么想法嗎?

這可能會給您一個起點

http://jsfiddle.net/JqT4k/1/

var str = "var sphere1 = CSG.sphere({center: [5, 5, 5], radius: 10, resolution: resolution }); var sphere2 = sphere1.translate([12, 5, 0]);";

//evaluates each replace individually
function match_eval(m){
    // m is going to be [5, 5, 5] and then [12, 5, 0], you can do with this as you see fit
    return "[0,0,0]";    
}

alert(str.replace(/\[( *\d+ *,?)+\]/g, match_eval));

我已經在這里修復它: http : //jsfiddle.net/jonnycowboy/w4n3W/12我使用了我在另一個stackoverflow問題/答案中找到的方法:'var nums = $(selector).html()。match(/ [ 0-9] + / g); var words = $(selector).html()。split(/ [0-9] + / g); newHtml + =''+ words [0] +''; for(var i = 0; i <nums.length; i ++){newHtml + =''+ nums [i] +''; newHtml + =''+單詞[i + 1] +''; }'–

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM