正则的简单使用

前段时间在smashing magazine上面看到了一篇关于正则使用的文章,将里面的代码自己敲了一遍。

const re = /Item(?= 10)/;

console.log(re.exec(‘Item’));
// → null

console.log(re.exec(‘Item5’));
// → null

console.log(re.exec(‘Item 5’));
// → null

console.log(re.exec(‘Item 10’));
// → [“Item”, index: 0, input: “Item 10”, groups: undefined]

const re = /Red(?!head)/;

console.log(re.exec(‘Redhead’));
// → null

console.log(re.exec(‘Redberry’));
// → [“Red”, index: 0, input: “Redberry”, groups: undefined]

console.log(re.exec(‘Redjay’));
// → [“Red”, index: 0, input: “Redjay”, groups: undefined]

console.log(re.exec(‘Red’));
// → [“Red”, index: 0, input: “Red”, groups: undefined]

const re = /(?<=€)\d+(.\d*)?/;

console.log(re.exec(‘199’));
// → null

console.log(re.exec(‘$199’));
// → null

console.log(re.exec(‘€199’));
// → [“199”, undefined, index: 1, input: “€199”, groups: undefined]

const re = /(?<!\d{3}) meters/;

console.log(re.exec(‘10 meters’));
// → [“ meters”, index: 2, input: “10 meters”, groups: undefined]

console.log(re.exec(‘100 meters’));
// → null

const re = /(?<=\d{2})(?<!35) meters/;

console.log(re.exec(‘35 meters’));
// → null

console.log(re.exec(‘meters’));
// → null

console.log(re.exec(‘4 meters’));
// → null

console.log(re.exec(‘14 meters’));
// → [“meters”, index: 2, input: “14 meters”, groups: undefined]

const re = /(\w+).jpg/;
const str = ‘File name: cat.jpg’;
const match = re.exec(str);
const fileName = match[1];

// The second element in the resulting array holds the portion of the string that parentheses matched
console.log(match);
// → [“cat.jpg”, “cat”, index: 11, input: “File name: cat.jpg”, groups: undefined]

console.log(fileName);
// → cat

const re = /(\d{4})-(\d{2})-(\d{2})/;
const match = re.exec(‘2020-03-04’);

console.log(match[0]); // → 2020-03-04
console.log(match[1]); // → 2020
console.log(match[2]); // → 03
console.log(match[3]); // → 04

const re = /(?\d{4})-(?\d{2})-(?\d{2})/;
const match = re.exec(‘2020-03-04’);

console.log(match.groups); // → {year: “2020”, month: “03”, day: “04”}
console.log(match.groups.year); // → 2020
console.log(match.groups.month); // → 03
console.log(match.groups.day); // → 04

const re = /(?\d{4})-(?\d{2})-(?\d{2})/;
const match = re.exec(‘2020-03-04’);

console.log(match[0]); // → 2020-03-04
console.log(match[1]); // → 2020
console.log(match[2]); // → 03
console.log(match[3]); // → 04

const re = /(?\d{4})-(?\d{2})-(?\d{2})/;
const [match, year, month, day] = re.exec(‘2020-03-04’);

console.log(match); // → 2020-03-04
console.log(year); // → 2020
console.log(month); // → 03
console.log(day); // → 04

const re = /\d+/;
const match = re.exec(‘123’);

console.log(‘groups’ in match); // → true

const re = /\d+(?st|nd|rd|th)?/;

let match = re.exec(‘2nd’);

console.log(‘ordinal’ in match.groups); // → true
console.log(match.groups.ordinal); // → nd

match = re.exec(‘2’);

console.log(‘ordinal’ in match.groups); // → true
console.log(match.groups.ordinal); // → undefined

console.log(/(\w\w)\1/.test(‘abab’)); // → true

// if the last two letters are not the same
// as the first two, the match will fail
console.log(/(\w\w)\1/.test(‘abcd’)); // → false

const re = /\b(?\w+)\s+\k\b/;

const match = re.exec(“I’m not lazy, I’m on on energy saving mode”);

console.log(match.index); // → 18
console.log(match[0]); // → on on

const re = /\b(?\w+)\s+\1\b/;

const match = re.exec(“I’m not lazy, I’m on on energy saving mode”);

console.log(match.index); // → 18
console.log(match[0]); // → on on

const re = /(?\d):\1:\k/;

const match = re.exec(‘5:5:5’);

console.log(match[0]); // → 5:5:5

const str = ‘War & Peace’;

console.log(str.replace(/(War) & (Peace)/, ‘$2 & $1’));
// → Peace & War

console.log(str.replace(/(?War) & (?Peace)/, ‘$ & $‘));
// → Peace & War

const str = ‘War & Peace’;

const result = str.replace(/(?War) & (?Peace)/, function(match, group1, group2, offset, string) {
return group2 + ‘ & ‘ + group1;
});

console.log(result); // → Peace & War

console.log(/./.test(‘\n’)); // → false
console.log(/./.test(‘\r’)); // → false

console.log(/[\w\W]/.test(‘\n’)); // → true
console.log(/[\w\W]/.test(‘\r’)); // → true

console.log(/./s.test(‘\n’)); // → true
console.log(/./s.test(‘\r’)); // → true

const str = ‘𝟠’;

console.log(/\d/.test(str)); // → false
console.log(/\d/u.test(str)); // → false

const str = ‘𝟠’;
console.log(/\p{Number}/u.test(str)); // → true

const str = ‘漢’;

console.log(/\p{Alphabetic}/u.test(str)); // → true

// the \w shorthand cannot match 漢
console.log(/\w/u.test(str)); // → false

console.log(/\P{Number}/u.test(‘𝟠’)); // → false
console.log(/\P{Number}/u.test(‘漢’)); // → true

console.log(/\P{Alphabetic}/u.test(‘𝟠’)); // → true
console.log(/\P{Alphabetic}/u.test(‘漢’)); // → false