2014-03-12 37 views
0
var text = "{templateUrl: 'conversations/conversations.tpl.html',"; 
var result = text.match(/templateUrl:(\s*['"]\S*['"])/g); 

我希望結果爲'conversations/conversations.tpl.html',但我得到的是「templateUrl:'conversations/conversations.tpl.html'」。如何在Javascript中提取參數的值正則表達式

我已經在這裏檢查過它:http://regex101.com/r/uW1vY6它做我期望的,但不是在控制檯或真正的程序中。

我在做什麼錯?

+0

的[正則表達式的Javascript和子匹配]可能重複(http://stackoverflow.com/questions/844001/javascript-regex-and-submatches) – Barmar

+0

@Barmar同意,謝謝 – kpg

回答

0

這正是你如何從正則表達式

var text = "{templateUrl: 'conversations/conversations.tpl.html',"; 
var myRegexp = /templateUrl:(\s*['"]\S*['"])/g; 
var match = myRegexp.exec(text); 
alert(match[1]); 

獲得匹配組試試這個

var text = "{templateUrl: 'conversations/conversations.tpl.html',"; 
var result = text.match(/\'(.*)\'/)[0]; 
console.log(result);