扫码关注官方订阅号
最近在重造一个ng-repeat的轮子,正则表达式实在有点难,只能在这里践行下拿来主义。要有实现嵌套循环的。 可以改造下:<p ng-repeat="v in vs">{{v}}</p endrepeat>
<p ng-repeat="v in vs">{{v}}</p endrepeat>
{{}}表达式我已经实现。 跪求原理或者您的方案,最好能说下一些正则的写法。
认证高级PHP讲师
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<code class="lang-javascript"><br>var match = expression.match(/^\s*([\s\S]+?)\s+in\s+([\s\S]+?)(?:\s+as\s+([\s\S]+?))?(?:\s+track\s+by\s+([\s\S]+?))?\s*$/);
<code
class
=
"lang-javascript"
><br>
var
match = expression.match(/^\s*([\s\S]+?)\s+in\s+([\s\S]+?)(?:\s+
as
\s+([\s\S]+?))?(?:\s+track\s+by\s+([\s\S]+?))?\s*$/);
if (!match) {
if
(!match) {
throw ngRepeatMinErr('iexp', "Expected expression in form of '_item_ in _collection_[ track by _id_]' but got '{0}'.",expression);
throw
ngRepeatMinErr(
'iexp'
,
"Expected expression in form of '_item_ in _collection_[ track by _id_]' but got '{0}'."
,expression);
}
var lhs = match[1];
lhs = match[1];
var rhs = match[2];
rhs = match[2];
var aliasAs = match[3];
aliasAs = match[3];
var trackByExp = match[4];
trackByExp = match[4];
match = lhs.match(/^(?:([\$\w]+)|\(([\$\w]+)\s*,\s*([\$\w]+)\))$/);
throw ngRepeatMinErr('iidexp', "'_item_' in '_item_ in _collection_' should be an identifier or '(_key_, _value_)' expression, but got '{0}'.",lhs);
'iidexp'
"'_item_' in '_item_ in _collection_' should be an identifier or '(_key_, _value_)' expression, but got '{0}'."
,lhs);
var valueIdentifier = match[3] || match[1];
valueIdentifier = match[3] || match[1];
var keyIdentifier = match[2];
keyIdentifier = match[2];
if (aliasAs && (!/^[$a-zA-Z_][$a-zA-Z0-9_]*$/.test(aliasAs) ||
(aliasAs && (!/^[
$a
-zA-Z_][
-zA-Z0-9_]*$/.test(aliasAs) ||
/^(null|undefined|this|\$index|\$first|\$middle|\$last|\$even|\$odd|\$parent)$/.test(aliasAs))) {
/^(null|undefined|this|\
$index
|\
$first
$middle
$last
$even
$odd
$parent
)$/.test(aliasAs))) {
throw ngRepeatMinErr('badident', "alias '{0}' is invalid --- must be a valid JS identifier which is not a reserved name.",aliasAs);
'badident'
"alias '{0}' is invalid --- must be a valid JS identifier which is not a reserved name."
,aliasAs);
</code>
这几段代码是用来判断ng-repeat表达式是否合法,并从中提取变量名的。。。反正我是没有耐心看这些正则表达式。
微信扫码关注PHP中文网服务号
QQ扫码加入技术交流群
扫描下载App
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
PHP学习
技术支持
返回顶部
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<code
class
=
"lang-javascript"
><br>
var
match = expression.match(/^\s*([\s\S]+?)\s+in\s+([\s\S]+?)(?:\s+
as
\s+([\s\S]+?))?(?:\s+track\s+by\s+([\s\S]+?))?\s*$/);
if
(!match) {
throw
ngRepeatMinErr(
'iexp'
,
"Expected expression in form of '_item_ in _collection_[ track by _id_]' but got '{0}'."
,expression);
}
var
lhs = match[1];
var
rhs = match[2];
var
aliasAs = match[3];
var
trackByExp = match[4];
match = lhs.match(/^(?:([\$\w]+)|\(([\$\w]+)\s*,\s*([\$\w]+)\))$/);
if
(!match) {
throw
ngRepeatMinErr(
'iidexp'
,
"'_item_' in '_item_ in _collection_' should be an identifier or '(_key_, _value_)' expression, but got '{0}'."
,lhs);
}
var
valueIdentifier = match[3] || match[1];
var
keyIdentifier = match[2];
if
(aliasAs && (!/^[
$a
-zA-Z_][
$a
-zA-Z0-9_]*$/.test(aliasAs) ||
/^(null|undefined|this|\
$index
|\
$first
|\
$middle
|\
$last
|\
$even
|\
$odd
|\
$parent
)$/.test(aliasAs))) {
throw
ngRepeatMinErr(
'badident'
,
"alias '{0}' is invalid --- must be a valid JS identifier which is not a reserved name."
,aliasAs);
}
</code>
这几段代码是用来判断ng-repeat表达式是否合法,并从中提取变量名的。。。反正我是没有耐心看这些正则表达式。