Reputation: 867
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
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:
search '-' char
if found - match a-z0-9# while not meet any space
need add condition, match only if # 0 or 1 times in pattern capture
may be like [a-z0-9#{0,1}] mean # 0 times or one...
Upvotes: 1
Views: 2276
Reputation: 15000
This expression will:
-
character #
to appear at most 1 time in the stringa-z0-9
characters#
character to appear anywhere in the string, including the beginning and end^(?!(?:.*?#){2,})-[a-z0-9#]+$
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
Samples
-abcdefghijklmnopqrstuvwxyz1234567890 = good
-abcdefghijklmnopq#rstuvwxyz1234567890 = good
-abcdefghijklmnopq##rstuvwxyz1234567890 = bad
-#abcdefghijklmnopqrstuvwxyz1234567890 = good
-##abcdefghijklmnopqrstuvwxyz1234567890 = bad
-#abcdefghijklmnopqrstuvwxyz1234567890 = good
-abcdefghijklmnopqrstuvwxyz1234567890# = good
-#abcdefghijklmnopqrstuvwxyz1234567890# = bad
#-abcdefghijklmnopqrstuvwxyz1234567890 = bad
Upvotes: 3
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
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
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