如何使用javascript将固定字符串与数字进行匹配

How to match a fixed string with digits with javascript?

本文关键字:数字 字符串 javascript 何使用      更新时间:2023-11-13

字符串为-Success0Success7或任何数字。我试过使用-

console.log(str.match("Success"+/'d/));

但它正在记录null。有什么帮助吗?

.match()中的正则表达式不正确。

Do:

var str = "Success123";
alert(str.match(/^Success'd+$/)); // returns the string if it is a match, otherwise null



请注意,如果您只是在测试字符串(如果它的开头有Success),那么您应该使用.test()。与CCD_ 8相比,CCD_。

var str = "Success123";
alert(/^Success'd+$/.test(str)); // returns true/false



Regex /^Success'd+$/对此进行了解释。

读数:

  • .test()|MDN
  • .match()|MDN

试试这个:

str.match(/Success'd+/)

假设你有数字,它是整个字符串:

str.match(/^Success'd+$/)

如果你想建立你的正则表达式:

var r= new RegExp("^Success"+'''d+$')
console.log(r.test('Success232'));