简体   繁体   English

如何检查字符串是否有星号后跟四位数字?

[英]how to check whether a string have stars followed by four digit number?

I have a random string which is getting from database. 我有一个从数据库获取的随机字符串。 A string might be any length. 字符串可以是任何长度。 I need to check whether a string should be in this pattern like *******1234 . 我需要检查一个字符串是否应该像*******1234这样的模式。 A string can have any number of stars (*) with four digit number. 一个字符串可以包含任意数量的星号(*)和四位数字。 How do I check that? 我该如何检查?

To check if a string starts with asterisks and ends with 4 digits, use this regexp: 要检查字符串是否以星号开头并以4位数结尾,请使用此正则表达式:

 const exp = new RegExp('^\\\\*+\\\\d{4}$'); console.log(exp.test('***1234')) // true; console.log(exp.test('***12345')) // false console.log(exp.test('****234')) // false console.log(exp.test('***d1234')) // false console.log(exp.test('a***1234')) // false 

  • ^ : start of string ^ :字符串的开头
  • \\*+ : match at least one asterisk \\*+ :匹配至少一个星号
  • \\d{4} : match exactly 4 digits \\d{4} :恰好匹配4位数
  • $ : end of string $ :字符串结束

You can try RegEx /^\\*{1,}\\d{4}/ . 您可以尝试RegEx /^\\*{1,}\\d{4}/

Where 哪里

^ starts the matching at the beginning of the string ^在字符串的开头开始匹配

\\*{1,} matches the character * literally between one and unlimited times, as many times as possible, giving back as needed (greedy) \\*{1,}在字面上与字符*匹配,在一次和无限次之间,尽可能多次,根据需要返回(贪婪)

\\d{4} matches a digit (equal to [0-9]) exactly 4 times \\d{4}恰好匹配一个数字(等于[0-9])4次

With RegExp.prototype.test() : 使用RegExp.prototype.test()

The test() method executes a search for a match between a regular expression and a specified string. test()方法执行搜索正则表达式和指定字符串之间的匹配。 Returns true or false . 返回truefalse

 function checkString(str){ return /^\\*{1,}\\d{4}/.test(str); } console.log(checkString('*******1234')); console.log(checkString('*******12')); console.log(checkString('34*******1234')); 

You can perform the task with two ways, First with regular expression and other with below : 您可以通过两种方式执行任务:第一种是正则表达式,另一种是下面的:

var id = "ctl03_Tabs1";
var lastFive = id.substr(id.length - 5); // => "Tabs1"
var lastChar = id.substr(id.length - 1); // => 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何显示用一串星号掩盖 rest 的任何输入数字的最后四位? - How to display the last four digit of any input number masking the rest with a string of stars? 如何检查我的字符串是否以 WA 后跟 9 位数字结尾 - How do I check if my string ends with WA followed by 9 digit 检查一个字符串是以一个字母开头,后跟数字,然后是任何字符 - Check if a string starts with one letter followed by digit(s) and then any characters 只显示字符串的最后四位数字 - Display only the last four digit number from a String 如何在开头匹配 0 或 + 后跟 1 到 3 位数字,但不能同时匹配两者? - How to match either 0 or + followed by 1 to 3 digit number at the beginning but not both? 如何使用 javascript 检查 var 是字符串还是数字 - how to check whether a var is string or number using javascript 如何在JS中为前2个字符编写正则表达式可以是数字或字母,然后是连字符,然后是6位数字? - How to write regex in JS for first 2 characters can be digit or alphabet and followed by hyphen and then 6 digit number? 检查输入字符串是否包含javascript中的数字 - Check whether an input string contains a number in javascript 检查JavaScript中变量是数字还是字符串 - Check whether variable is number or string in JavaScript 如何检查数字是否多次使用 - How to check if a digit is used in a number multiple times
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM