javascript 屏蔽javascript变量值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17650197/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Mask javascript variable value
提问by Pawan
I want to mask my javascript variable value.Basically i am getting phone number fields value in variable and after getting it i want to mask it in some other format.
我想屏蔽我的 javascript 变量值。基本上我在变量中获取电话号码字段值,在获取它后我想以其他格式屏蔽它。
I have tried the jquery Mask pluginbut its mask the control's string format.But i want to mask the string into the variable.
我已经尝试了jquery Mask 插件,但它屏蔽了控件的字符串格式。但我想将字符串屏蔽到变量中。
Ex:
前任:
Let i have a phone string "0000000000"
and i am storing it in a variable :-
让我有一个电话字符串"0000000000"
,并将其存储在一个变量中:-
var number ="0000000000"
and after that i want to mask it in other formats.Like-
之后我想用其他格式屏蔽它。喜欢-
1) number = number.mask('000-0000'); 2) number = number.mask('(000) 000-0000');
1) number = number.mask('000-0000'); 2) number = number.mask('(000) 000-0000');
回答by German Latorre
There is a similar question (Javascript phone mask for text field with regex).
有一个类似的问题(带有正则表达式的文本字段的 Javascript 电话掩码)。
You can do it using regular expressions (see the code working at http://jsfiddle.net/BBeWN/45/):
您可以使用正则表达式(请参阅http://jsfiddle.net/BBeWN/45/ 上的代码):
var value = '1234567';
var formatted = value.replace(/^(\d{3})(\d{4}).*/, '-');
Notice that each part of the regular expression that is wrapped within parenthesis, (\d{3})
and (\d{4})
in the example above, can then be referenced to build the formatted text string using $1
and $2
respectively.
请注意,括号内的正则表达式的每个部分((\d{3})
以及(\d{4})
在上面的示例中)都可以被引用以分别使用$1
和构建格式化的文本字符串$2
。
If you have N parts of the regular expression wrapped within parenthesis, you would be able to reference them to build a formatted text string with $1
, $2
, ..., $N
respectively.
如果正则表达式的 N 部分包含在括号内,则可以分别引用它们以构建带有$1
, $2
, ...的格式化文本字符串$N
。
So, for the other format you mention ((000) 000-0000), the code would be like this:
因此,对于您提到的其他格式 ((000) 000-0000),代码将如下所示:
var formatted = value.replace(/^(\d{3})(\d{3})(\d{4}).*/, '() -');
You can find a great JavaScript regular expressions reference at https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
您可以在https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions找到一个很棒的 JavaScript 正则表达式参考
回答by Ivan Kukushkin
There is a javascript library that provides a solution for this problem:
有一个 javascript 库为这个问题提供了解决方案:
https://github.com/the-darc/string-mask
https://github.com/the-darc/string-mask
And you don't need to create a new regular expression every time you change your mask(Format).
并且每次更改掩码(格式)时都不需要创建新的正则表达式。
You can use it just like that:
你可以这样使用它:
var formatter = new StringMask("(000) 000-0000", { reverse: true });
var result = formatter.apply('123456789'); // (012) 345-6789
or
或者
var formatter = new StringMask('#.##0,00', { reverse: true });
var result = formatter.apply('123456789'); // 1.234.567,89
回答by matt3141
Here is a "mask" you may apply to a string of phone number digits of length 7 or 10 with an optional "+" to render it with dashes.
这是一个“掩码”,您可以将其应用于一串长度为 7 或 10 的电话号码数字,并带有可选的“+”以将其呈现为破折号。
number.match(/^\+?([0-9]{3}){1,2}([0-9]{4})$/) || []).join('-')
回答by Paul Hodel
Using jsuites plugin you can do that
使用 jsuites 插件你可以做到
A) HTML
一)HTML
<html>
<script src="https://bossanova.uk/jsuites/v2/jsuites.js"></script>
<link rel="stylesheet" href="https://bossanova.uk/jsuites/v2/jsuites.css" type="text/css" />
<input data-mask='+44 0000 000 000'>
</html>
B) JAVASCRIPT
B) JAVASCRIPT
To get the parse a string into a variable
将字符串解析为变量
<script>
var test = jSuites.mask.run('123456789', '(000) 000-0000');
console.log(test);
</script>
回答by jsonmaur
You can use my package https://github.com/jsonmaur/coverup, which can return the masked string as a variable.
您可以使用我的包https://github.com/jsonmaur/coverup,它可以将掩码字符串作为变量返回。
回答by Dmitry
I believe what you are looking for you can find on the apple.com support page. Try to schedule a call and check out their phone input field. It adjusts presentation based on the number of digits you are entering. Looking at the source, (seems like its somewhere here) this one has the keyup event for the field and looks like they are parsing on keyup.
我相信您可以在 apple.com 支持页面上找到您正在寻找的内容。尝试安排通话并查看他们的电话输入字段。它会根据您输入的位数调整演示文稿。查看源代码,(似乎它在这里的某个地方)这个具有该字段的 keyup 事件,看起来他们正在解析 keyup。