Check If first character is "+"

11k views Asked by At

How can i detect string is start with "+"

I tried
^\s*?\+.*$
but no help.

P.s: I have only one line alltime.

4

There are 4 answers

0
Federico Piazza On BEST ANSWER

You don't need \s*?, you have to use:

^\+
or...
^[+]

In case you want to check a complete string, you can use:

^\+.*$

Working demo

0
X3074861X On

^\+.*$ should work for your purposes.

Here's a fiddle with a couple test strings : https://regex101.com/r/nP2eL7/1

0
Devin B. On

Here's an optional (and optimal) solution in the case that the first character of your string happens to be either a + or - and you don't want the proceeding number to have any leading zeros:

/(?<=^\+|-|^)[1-9]\d*/
0
hkchakladar On

Without regex, you can also use native method startsWith().

So it would be:

var str1 = '+some text';
var bool = str1.startsWith('+'); //true