Dmitry
Dmitry

Reputation: 867

regex string contains max one '#'

Need regex to match strings by pattern exclude if '#' more then 1 times in string

var srg = new RegExp(/([a-z0-9#]+[\s])/g);

Strings:

fdsffmsd,fmsd 
qwjswkds03sj 
ewew 
rfekwjkr#jfkdlsf 
wiru0ksd#erjk#jkls 
# 
casdw##kfdl 

Result

fdsffmsd,fmsd

qwjswkds03sj

ewew

rfekwjkr#jfkdlsf

wiru0ksd#erjk#jkls

#

casdw##kfdl

Fiddle

Thanks

UPDATE:

Excuse me for not good task explanation

I update new fiddle New fiddle

var srg = new RegExp(/((\-)[a-z0-9#]+[\s])/g);

What we do:

may be like [a-z0-9#{0,1}] mean # 0 times or one...

Upvotes: 1

Views: 2276

Answers (4)

Ro Yo Mi
Ro Yo Mi

Reputation: 15000

Description

This expression will:

  • require the string to start with a - character
  • allows the # to appear at most 1 time in the string
  • prevents a space from appearing in the string
  • allows one or more of a-z0-9 characters
  • allows the # character to appear anywhere in the string, including the beginning and end

^(?!(?:.*?#){2,})-[a-z0-9#]+$

enter image description here

NODE                     EXPLANATION
--------------------------------------------------------------------------------
  ^                        the beginning of the string
--------------------------------------------------------------------------------
  (?!                      look ahead to see if there is not:
--------------------------------------------------------------------------------
    (?:                      group, but do not capture (at least 2
                             times (matching the most amount
                             possible)):
--------------------------------------------------------------------------------
      .*?                      any character except \n (0 or more
                               times (matching the least amount
                               possible))
--------------------------------------------------------------------------------
      #                        '#'
--------------------------------------------------------------------------------
    ){2,}                    end of grouping
--------------------------------------------------------------------------------
  )                        end of look-ahead
--------------------------------------------------------------------------------
  -                        '-'
--------------------------------------------------------------------------------
  [a-z0-9#]+               any character of: 'a' to 'z', '0' to '9',
                           '#' (1 or more times (matching the most
                           amount possible))
--------------------------------------------------------------------------------
  $                        before an optional \n, and the end of the
                           string

Examples

Live Demo

Samples

-abcdefghijklmnopqrstuvwxyz1234567890     = good
-abcdefghijklmnopq#rstuvwxyz1234567890    = good
-abcdefghijklmnopq##rstuvwxyz1234567890   = bad
-#abcdefghijklmnopqrstuvwxyz1234567890    = good
-##abcdefghijklmnopqrstuvwxyz1234567890   = bad
-#abcdefghijklmnopqrstuvwxyz1234567890    = good
-abcdefghijklmnopqrstuvwxyz1234567890#    = good
-#abcdefghijklmnopqrstuvwxyz1234567890#   = bad
#-abcdefghijklmnopqrstuvwxyz1234567890    = bad

Upvotes: 3

Steve Chambers
Steve Chambers

Reputation: 39404

First attempt:

var srg = new RegExp(/[a-z0-9]*#?[a-z0-9]*\s/g);

Explanation: #? specifies that a # character can occur once or not at all. This is sandwiched between two [a-z0-9]* expressions, which allow alphanumerics before and after. The final \s specifies a single whitespace character must occur afterwards (consistent with your original regex).

EDIT - Second attempt:

var srg = new RegExp(/([a-z0-9]+#?[a-z0-9]*|[a-z0-9]*#?[a-z0-9]+)\s/g);

This looks more complex but all it's doing is taking the regex above and splitting into two possibilities - the first where there must be at least one alphanumeric before the # and the second where there must be at least one alphanumeric after the #.

Upvotes: 1

anubhava
anubhava

Reputation: 785058

EDIT: Based on your updates:

You can use this regex to allow max one # in a string:

var srg = new RegExp('^-[a-z0-9]+#?[a-z0-9]+$');

'-wiru0ksd#erjk#jkls'.match(srg); //=> null

'-fdsffmsd,fmsd'.match(srg); //=> null

'-rfekwjkr#jfkdlsf'.match(srg); //=> ["-rfekwjkr#jfkdlsf"]

Upvotes: 2

dirtydexter
dirtydexter

Reputation: 1073

what you can do is call 'your line'.match(/#/g).length in you method and if the length is greater than 1 you can exclude those results. after that perform your match if this condition satisfies, by chaining.

Upvotes: 1

Related Questions