6

How can i detect string is start with "+"

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

P.s: I have only one line alltime.

Black White
  • 700
  • 3
  • 11
  • 31

4 Answers4

9

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

^\+
or...
^[+]

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

^\+.*$

Working demo

Federico Piazza
  • 30,085
  • 15
  • 87
  • 123
1

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

So it would be:

var str1 = '+some text';
var bool = str1.startsWith('+'); //true
hkchakladar
  • 749
  • 1
  • 7
  • 15
0

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

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

X3074861X
  • 3,709
  • 5
  • 32
  • 45
0

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*/
Devin B.
  • 433
  • 4
  • 18