我正在尝试找到一个正则表达式解决方案,以检查字符串是否符合所有条件+ /
的正斜杠。
当前代码:
var specialChars = /^[a-zA-Z0-9!@#\$%\^\&*\)\(+=._-]+$/g;
如果字符串看起来像4!@#$
,则将匹配true。
但是,如果字符串如下所示,则它不起作用:5/6/2019
这就是我执行此检查的方式,基本上我有一个接受长字符串的函数。而我想做的就是拔出跟踪ID,然后从中创建一个链接。
我的测试用例也在演示中,日期测试失败了,因为linkCreator函数最终链接到了日期:
https://jsfiddle.net/cojuevp5/
var linkCreator = function(value) {
var strings = value.split(' ');
var aHref = '<a href="http://www.google.com/search?q=';
var targetBlank = '" target="_blank" style="text-decoration: underline">';
var trackingString = strings.reduce(function(prevVal, currVal, idx) {
var specialChars = /^[a-zA-Z0-9!@#\$%\^\&*\)\(+=._-]+$/g;
// Does val start with number and not contain special characters including /
var link = currVal.match(/^\d/) && !currVal.match(specialChars) ?
aHref + currVal + targetBlank + currVal + '</a>' :
currVal;
return idx == 0 ? link : prevVal + ' ' + link;
}, '');
console.log(trackingString);
}
const case1 = '434663008870'
const case2 = '4S4663008870'
const case3 = '4S4663008870 PS'
const case4 = 'SHD FX 462367757727 PS'
const case5 = 'SHD FX 429970755485, R'
const case6 = 'SHD HEADER TRACKING PS'
const case7 = 'N/A'
const case8 = 'AF SHD FX 462367757727 PS'
const case9 = '4/7/2019'
const case10 = '4!@#$%^&'
const value = case9
const link = linkCreator(value)
console.log(link)