2016-05-26 29 views
1

我的正則表達式模式必須以/ *開始並且必須以* /結尾;在它之間它可能包含所有字母,數字,特殊字符 - 零次或多次。使用預定義的開始和結束模式的正則表達式

我已經做了以下的正則表達式表達了這一點:

[/*][[email protected]#\^\$&\*\(\)-_\+=\[\]\{\}\|\\,\.\?\s]*[*/;] 

但這種表達不顯示模式,如錯誤:

  1. /*
  2. /* sdfsdff
  3. /* sdfsfeff *
  4. /* fefef3323 */

這是錯誤的。它必須以/ *開頭並以* /結尾;不惜任何代價。

以下是用於測試此模式的角碼。請有人幫忙!

CODE:

<html> 

<head> 
     <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.8/angular.min.js" ></script> 
     <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.8/angular-messages.min.js"></script> 
</head> 

<body ng-app="myApp" ng-controller="myCtrl"> 

<form name="form1" novalidate> 
    {{form1.age.$error}} 
    <input type="text" name="age" ng-model="myAge" ng-pattern="/^[/*][[email protected]#\^\$&\*\(\)-_\+=\[\]\{\}\|\\,\.\?\s]*[*/;]$/" /> 
    <div ng-messages="form1.age.$error" > 
     <span ng-message="pattern">This field has wrong pattern.</span> 
    </div> 
</form> 

<script> 
//module declaration 
var app = angular.module("myApp",['ngMessages']); 
//controller declaration 
app.controller('myCtrl',function($scope){ 
    //code goes here ... 
}); 
</script> 

</body> 

</html> 

參考:

Regular expression include and exclude special characters

http://www.regular-expressions.info/repeat.html

+1

從開始和結束中刪除字符類 – rock321987

+1

'[/] [*] [a-zA-Z0-9〜@#\^\ $ \ \ * \(\)-_ \ + = \ [\] {\} \ | \\,\。\?\ s] * [*] [/] [;]' – rock321987

+0

嘗試http:// regexr。COM /爲您的正則表達式 – NatureShade

回答

3

支架裝置任何所含炭。因此,改變你的正則表達式:

\/\*[[email protected]#\^\$&\*\(\)-_\+=\[\]\{\}\|\\,\.\?\s]*\*\/; 

您可以閱讀Regex charclass


旁註:

  • 你不需要逃避.?()*{}類內
  • a-zA-Z0-9_等同於\ w。
  • 一個字符類中,-指一個範圍,從而\)-_從方式)_

我會寫:

\/\*[-\[email protected]#^$&*()+=\[\]{}|\\,.?\s]*\*\/; 

或者,如果你想捕捉什麼(甚至多-lines評論):

\/\*[\w\W]*?\*\/; 

demo

相關問題