admin管理员组文章数量:1401184
My title can look like
(10) - lorem ipsum
(101) - lorem ipsum
(1) - lorem ipsum
lorem ipsum
I want to check if my title contains (number)
at the start
string.match(/^(+[0-9]+)+$/);
I'm not good on regex, can someone help and say what is wrong ?
My title can look like
(10) - lorem ipsum
(101) - lorem ipsum
(1) - lorem ipsum
lorem ipsum
I want to check if my title contains (number)
at the start
string.match(/^(+[0-9]+)+$/);
I'm not good on regex, can someone help and say what is wrong ?
Share Improve this question edited Feb 29, 2016 at 9:44 Dave R. 7,3033 gold badges32 silver badges53 bronze badges asked Feb 29, 2016 at 9:40 WizardWizard 11.3k38 gold badges99 silver badges167 bronze badges 2-
1
If you want to just check, use
RegExp.test()
rather thanString.match()
unless you need the value itself. – Wiktor Stribiżew Commented Feb 29, 2016 at 9:44 -
Adam's answer should be correct, will require
(
to be first character on the string, followed by 1 or more numbers and then)
. @WiktorStribiżew I usually use this idiom;if( /^\[0-9]+\)/.test(string) )
– mschr Commented Feb 29, 2016 at 9:51
2 Answers
Reset to default 8Just remove the $
from your regex, and escape the parentheses.
string.match(/^\([0-9]+\)/);
$
means end of string.
(
and )
are special character and should be escaped. Pharenthesis are used for grouping. You can find a list of special characters here.
Use test method to return whether condition is true or false, Whether it starting with number or not.
/^\([0-9]+\)/.test(string);
OR
var patt = new RegExp(/^\([0-9]+\)/);
patt.test(string);
本文标签: javascriptCheck if a string starts with a number in parenthesesStack Overflow
版权声明:本文标题:javascript - Check if a string starts with a number in parentheses - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744270723a2598170.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论