资料参考:正则表达式
1、匹配指定字符串开始,例如:
const str1 = "LITERALLY CAN'T EVEN";
const str2 = "FIGURATIVELY can't even";
const str3 = "litERALLY whatever"
const reg = /^LITERALLY/
console.log(reg.test(str1)) // true
console.log(reg.test(str2)) // false
console.log(reg.test(str3)) // false
2、匹配指定字符串结束,例如:
const str1 = "THIS SHIITAKE IS BANANAS";
const str2 = "BANANAS";
const str3 = "NOTHING";
const reg = /BANANAS$/;
console.log(reg.test(str1)); // true
console.log(reg.test(str2)); // true
console.log(reg.test(str3)); // false
3、匹配开头非数字,第二个字符非大写字母,例如:
const str1 = "1A";
const str2 = "A1";
const str3 = "ABC";
const str4 = "abc";
const str5 = "555";
const str6 = "5z";
const str7 = "...";
const reg = /^\D[^A-D]/;
console.log(reg.test(str1)); // false
console.log(reg.test(str2)); // true
console.log(reg.test(str3)); // false
console.log(reg.test(str4)); // true
console.log(reg.test(str5)); // false
console.log(reg.test(str6)); // false
console.log(reg.test(str7)); // true