简体   繁体   English

如何防止我的函数修改常量中的字符串?

[英]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 自行完成(使用反斜杠作为转义字符)

Check this fiddle for yourself自己检查这个小提琴

\\ RegExPattern: '^\\\\d{9}$'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何防止我的平滑滚动jquery函数修改div元素的内容? - How do I prevent my smooth-scroll jquery function from modifying the contents of my div elements? 如何防止此函数多次修改变量? - How do I prevent this function from modifying the variable more than once? 如何防止从特定承诺中调用函数? - How do I prevent a function from being called from within a specific promise? 如何获取Ajax返回函数并阻止文档下载onclick - how do I get my Ajax to return a function and prevent my document from downloading onclick 如何防止我的 node.js 错误中间件 function 在随后的 function 调用中失败? - How do I prevent my node.js error middleware function from failing on subsequent function calls? 如何添加以防止正则表达式解析字符串的某些部分? - How do I add prevent my regex from parsing certain parts of a string? 如何防止我的程序跳过 html2pdf function? - How do I prevent my program from skipping html2pdf function? 如何防止我的触摸设备执行:: hover而不是单击功能? - How do I prevent my a touch device from executing an ::hover instead of a click function? 在我的函数运行时如何解除绑定或阻止其他单击事件发生? - How do I unbind or prevent other click events from occurring while my function is running? 如何防止 PHP sleep function 停止我的 HTML 页面 - How do I prevent PHP sleep function from stopping my HTML page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM