您的位置:首页 > Web前端 > JavaScript

Codewars-Javascript训练手册:正则表达式(上)

2016-04-27 01:57 435 查看

Autocomplete! Yay!(字符串自动补全)

题目描述: The autocomplete function will take in an input string and a dictionary array and return the values from the dictionary that start with the input string. If there are more than 5 matches, restrict your output to the first 5 results. If there are no matches, return an empty array.(autocomplete 函数输入一个字符串和字典数组,返回开头是该字符串的值,返回值的个数最多限制为5个,无匹配则返回空数组)

对于输入字符串的要求:Any input that is NOT a letter should be treated as if it is not there. For example, an input of “$%^” should be treated as “” and an input of “ab*&1cd” should be treated as “abcd”.

Example:

autocomplete('ai', ['airplane','airport','apple','ball']) = ['airplane','airport']


初始解决方案:

function autocomplete(input, dictionary){
var str = input.replace(/[^A-Za-z]+/g,'');
var pattern = new RegExp('^'+str,'i');
var temp = dictionary.filter(function(value){
if(pattern.test(value)){return true;
}else{
return false;
}
});
return temp.length<6?temp:temp.slice(0,5);
}


知识点:创建一个正则表达式有两种方式,一是直接量语法
var reg = /pattern/
适用于正常输入的正则表达式,二是创建 RegExp 对象,语法是new RegExp(pattern, attributes);例如:
var reg = new  RegExp('pattern','gi')
第二种语法适用于对正则表达式的拼接。注意第一种语法直接输入正则的内容即可,解释器通过
/×××/
这样的形式确认类型为RegExp对象。而第二种语法则类似字符串的写法。

解法:

//正则表达式的变量pattern可以简写为:
var pattern = new RegExp('^'+input.replace(/[^A-Za-z]+/g,''),'i')
//Array的slice语法中的第二个参数可以大于Array的长度,对输出无影响,例:
var tt = ['a','b'];
console.log(tt.slice(0,5));//输出为 ["a", "b"]
//因此可以简化最后一步的判断输出结果是否超过5个值
return dictionary.filter(function(w){ return r.test(w); }).slice(0, 5);


所以,最简的代码是:

function autocomplete(input, dictionary){
var r = new RegExp('^' + input.replace(/[^a-z]/gi,''), 'i');
return dictionary.filter(function(w){ return r.test(w); }).slice(0, 5);
}


这正是Codewars上得票最高的答案。

Credit Card Mask(信用卡字符加密)

题目描述: 给出一个函数maskify用“#”字符替换超过4个字符的字符串,4个字符则返回自身。例如:

maskify("4556364607935616") == "############5616"
maskify(     "64607935616") ==      "#######5616"
maskify(               "1") ==                "1"
maskify(                "") ==                 ""


本以为可以用一个正则表达式可以解决问题,然而发现
RegExp {X}
量词中x必须为数字。于是将原字符串分割开进行匹配,代码如下:

解法:

function maskify(cc) {
var len = cc.length;
if(len<5){
return cc;
}else{
//对字符串分割后进行正则匹配
return cc.slice(0,len-4).replace(/\w/g,'#') + cc.slice(len-4);
}
}


然而,我忘了正则的另外一个用法:
/regexp(?=n)/
?=n 量词匹配任何其后紧接指定字符串 n 的字符串,但匹配的内容并不包括指定字符串n。

所以,Codewars最简短的代码正是如此:

function maskify(cc) {
return cc.replace(/.(?=....)/g, '#');
//也可写出cc.replace(/.(?=.{4})/g, '#')
}


Formatting a number as price

**题目描述:**Your objective is to add formatting to a plain number to display it as price.

The function should return a string like this:

var price = numberToPrice(13253.5123);
console.log(price); // 13,253.51


Numbers should use the standard comma for every 3 numbers and dot to separate the cents, cents need to be truncated to 2 decimals, in the case that the decimal part of the number is 1 character long or none you should add 0’s so that the result will always have 2 decimal characters, the function will also evaluate negative numbers.

function should return a string ‘NaN’ if the input is not a valid number

简而言之,就是将给定的数值转化为货币格式,如果传入的参数不为数值类型,则返回NaN,注意负值的转化。这里有一个特殊点在于如果是超过3位小数,则保留两位小数,但并不进行四舍五入的操作。

解法:

var numberToPrice = function(number) {
if(typeof number !== 'number')return 'NaN';
var neg = number < 0 ? '-' : '';
var tempPrice = Math.abs(number).toFixed(3).slice(0, -1);
var subPrice = tempPrice.slice(0,-3);
var mod = subPrice.length > 3 ? subPrice.length % 3 : 0;
var resultStr = '';
resultStr = neg + (mod ? (subPrice.substr(0, mod) + ',') : '') + subPrice.substr(mod).replace(/(\d{3})(?=\d)/g, '$1' + ',') + tempPrice.substr(-3);
return resultStr;
}


算法写的不是很满意,给出codewars上投票最高的代码:

var numberToPrice = function(n) {
return typeof n != 'number' ? 'NaN' : n.toFixed(3).replace(/\d$/, '').replace(/(\d)(?=(?:\d{3})+\.)/g, '$1,')
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  正则表达式 函数