我正致力于创建自定义标记语法,并尝试使用特殊字符匹配某些字符串。例如,//Some text//
与正则表达式\/\/
匹配得很好。
表达式\/\/
在大多数情况下都有效,除非文本中有网址并将所有内容都删掉。如果在双斜杠之前有p:
或s:
,则如何排除/处理案例,例如在网址中:
http://或https://
我打算排除p://
和s://
,但不确定如何排除var source = [{
"PM": "Jane",
"e": "j@nunya.com",
"h": "15.00",
"w": "10/30/2016 12:00:00 AM",
"c": "John",
"p": "Happy Town USA"
}, {
"PM": "Jane",
"e": "j@nunya.com",
"h": "11.00",
"w": "11/06/2016 12:00:00 AM",
"c": "John",
"p": "Happy Town USA"
}, {
"PM": "Jill",
"e": "j@nunya.com",
"h": "21.00",
"w": "10/30/2016 12:00:00 AM",
"c": "John",
"p": "Sad Town USA"
}, {
"PM": "Jill",
"e": "j@nunya.com",
"h": "12.00",
"w": "11/06/2016 12:00:00 AM",
"c": "John",
"p": "Sad Town USA"
}];
// First we'll transform the source into a dictionary
var dict = source.reduce(function(p, c) {
// We'll use the combination of PM,e,c and p to build a key
var key = [c.PM, c.e, c.c, c.p].join("-");
if (p[key]) {
// If we've seen this key before, we'll just add to the Details
p[key].Details.push({
w: c.w,
h: c.h
});
} else {
// otherwise we create a new entry and populate it
p[key] = {
PM: c.PM,
c: c.c,
e: c.e,
p: c.p,
Details: [{
w: c.w,
h: c.h
}]
}
}
return p;
}, {});
// We now have a dictionary
console.log(dict);
// If you really need an array (and don't care about order), we can transform
// the dictonary to an array (order is not guaranteed here)
var result = Object.keys(dict).map(function(a) {
return dict[a];
});
console.log(result);
和0–127
。
提前谢谢!
答案 0 :(得分:1)
你可以使用负面的lookbehind正则表达式:
(?<!https:|http:)//.*?//
如果我们在(?<!https:|http:)
之前https:
或http:
,那么 //
是负面的背后断言将会失败