2016-10-26 10 views
-1

を作成し正規表現マッチングは、私が検証されている次の正規表現を持っているヌル

var cTextVal = "This URL should match http://google.com"; 
var regEx = "(http|ftp|https)://([\w_-]+(?:(?:\.[\w_-]+)+))([\w.,@?^=%&:/~+#-]*[\[email protected]?^=%&/~+#-])?" 


var matches = cTextVal.match(regEx); 
alert(matches); // This produces null 

どのように私はJavaScriptでこの正規表現にマッチする文字列を見つけるのですか?コメントに基づいて

更新:これはヌル生成

var regEx = /(http|ftp|https)://([\w_-]+(?:(?:\.[\w_-]+)+))([\w.,@?^=%&:/~+#-]*[\[email protected]?^=%&/~+#-])?/g 

::これは私のコードクラッシュ

前方

var regEx = "/(http|ftp|https)://([\w_-]+(?:(?:\.[\w_-]+)+))([\w.,@?^=%&:/~+#-]*[\[email protected]?^=%&/~+#-])?/g" 
+1

あなたは、そうでなければ、バックスラッシュをエスケープする必要はなく、 ' "yourRegex"'、 '/ yourRegex /'を使用する必要があります。 – Xufox

+1

または新しい正規表現....が、その後\ 'sのエスケープすることがが 'ヌルに基づいていない1が、あった場合、それは全体の一致のために'マッチ[0] 'でなり、その場合 –

+0

の痛みです'結果。 – PHPglue

答えて

0

エスケープは、第二のキャプチャグループの前にスラッシュ

var regEx = /(http|ftp|https):\/\/([\w_-]+(?:(?:\.[\w_-]+)+))([\w.,@?^=%&:/~+#-]*[\[email protected]?^=%&/~+#-])?/; 
 

 
var cTextVal = "This URL should match http://google.com"; 
 

 
var matches = cTextVal.match(regEx).shift(); 
 

 
console.log(matches);

関連する問題