[英]How do I prevent my function from modifying a string within a constant?
I have an issue with a RegEx pattern collection I am setting, along with many other resources, within an angular constant.我在一个角度常数内设置的 RegEx 模式集合以及许多其他资源有问题。 In this example, the RegExPattern has a '\\' character, which angular is stripping from the string.
在这个例子中,RegExPattern 有一个 '\\' 字符,它是从字符串中剥离出来的。 This renders the pattern useless.
这使得模式无用。
http://jsfiddle.net/beauxjames/u8sevn6k/ http://jsfiddle.net/beauxjames/u8sevn6k/
.constant('appConfig', {
RegExPattern: '^\d{9}$'
})
I am familiar with $sce and safely encoding HTML, but not from within a constant.我熟悉 $sce 并安全地编码 HTML,但不是来自常量。
What are my options here?我在这里有哪些选择?
Backslashes have to be escaped in strings...反斜杠必须在字符串中转义...
.constant('appConfig', {
RegExPattern: '^\\d{9}$'
})
Or you can use a regex literal或者您可以使用正则表达式文字
.constant('appConfig', {
RegExPattern: /^\d{9}$/
})
EDIT:编辑:
This has nothing to do with angular, javascript does this by itself (uses backslash as escape character)这与 angular 无关,javascript 自行完成(使用反斜杠作为转义字符)
双\\
RegExPattern: '^\\\\d{9}$'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.