RegEx pattern to not allow special character except underscore
I have a special requirement, where i need the achieve the following
- No Special Character is allowed except
_
in between string. - string should not start or end with
_
,.
and numeric value. - underscore should not be allowed before or after any numeric value.
I am able to achieve most of it, but my RegEx pattern is also allowing other special characters.
How can i modify the below RegEx pattern to not allow any special character apart from underscore that to in between strings.
^[^0-9._]*[a-zA-Z0-9_]*[^0-9._]$
javascript regex
add a comment |
I have a special requirement, where i need the achieve the following
- No Special Character is allowed except
_
in between string. - string should not start or end with
_
,.
and numeric value. - underscore should not be allowed before or after any numeric value.
I am able to achieve most of it, but my RegEx pattern is also allowing other special characters.
How can i modify the below RegEx pattern to not allow any special character apart from underscore that to in between strings.
^[^0-9._]*[a-zA-Z0-9_]*[^0-9._]$
javascript regex
Also, below pattern works for me, but i know for sure it's not the best solution.^[^0-9`|~|!|@|#|$|%|^|&|*|(|)|+|=|[||||\|'|<|,|.|>|?|/|""|;|:|s]*[a-zA-Z0-9_]*[^`|~|!|@|#|$|%|^|&|*|(|)|+|=|[||||\|'|<|,|.|>|?|/|""|;|:|s]*[^0-9`|~|!|@|#|$|%|^|&|*|(|)|+|=|[||||\|'|<|,|.|>|?|/|""|;|:|s]$
– shubham deodia
Nov 14 '18 at 13:08
Try/^(?=[A-Z])(?=.*[A-Z]$)(?!.*_d)(?!.*d_)w+$/i
– Wiktor Stribiżew
Nov 14 '18 at 14:04
add a comment |
I have a special requirement, where i need the achieve the following
- No Special Character is allowed except
_
in between string. - string should not start or end with
_
,.
and numeric value. - underscore should not be allowed before or after any numeric value.
I am able to achieve most of it, but my RegEx pattern is also allowing other special characters.
How can i modify the below RegEx pattern to not allow any special character apart from underscore that to in between strings.
^[^0-9._]*[a-zA-Z0-9_]*[^0-9._]$
javascript regex
I have a special requirement, where i need the achieve the following
- No Special Character is allowed except
_
in between string. - string should not start or end with
_
,.
and numeric value. - underscore should not be allowed before or after any numeric value.
I am able to achieve most of it, but my RegEx pattern is also allowing other special characters.
How can i modify the below RegEx pattern to not allow any special character apart from underscore that to in between strings.
^[^0-9._]*[a-zA-Z0-9_]*[^0-9._]$
javascript regex
javascript regex
asked Nov 14 '18 at 13:04
shubham deodiashubham deodia
52313
52313
Also, below pattern works for me, but i know for sure it's not the best solution.^[^0-9`|~|!|@|#|$|%|^|&|*|(|)|+|=|[||||\|'|<|,|.|>|?|/|""|;|:|s]*[a-zA-Z0-9_]*[^`|~|!|@|#|$|%|^|&|*|(|)|+|=|[||||\|'|<|,|.|>|?|/|""|;|:|s]*[^0-9`|~|!|@|#|$|%|^|&|*|(|)|+|=|[||||\|'|<|,|.|>|?|/|""|;|:|s]$
– shubham deodia
Nov 14 '18 at 13:08
Try/^(?=[A-Z])(?=.*[A-Z]$)(?!.*_d)(?!.*d_)w+$/i
– Wiktor Stribiżew
Nov 14 '18 at 14:04
add a comment |
Also, below pattern works for me, but i know for sure it's not the best solution.^[^0-9`|~|!|@|#|$|%|^|&|*|(|)|+|=|[||||\|'|<|,|.|>|?|/|""|;|:|s]*[a-zA-Z0-9_]*[^`|~|!|@|#|$|%|^|&|*|(|)|+|=|[||||\|'|<|,|.|>|?|/|""|;|:|s]*[^0-9`|~|!|@|#|$|%|^|&|*|(|)|+|=|[||||\|'|<|,|.|>|?|/|""|;|:|s]$
– shubham deodia
Nov 14 '18 at 13:08
Try/^(?=[A-Z])(?=.*[A-Z]$)(?!.*_d)(?!.*d_)w+$/i
– Wiktor Stribiżew
Nov 14 '18 at 14:04
Also, below pattern works for me, but i know for sure it's not the best solution.
^[^0-9`|~|!|@|#|$|%|^|&|*|(|)|+|=|[||||\|'|<|,|.|>|?|/|""|;|:|s]*[a-zA-Z0-9_]*[^`|~|!|@|#|$|%|^|&|*|(|)|+|=|[||||\|'|<|,|.|>|?|/|""|;|:|s]*[^0-9`|~|!|@|#|$|%|^|&|*|(|)|+|=|[||||\|'|<|,|.|>|?|/|""|;|:|s]$
– shubham deodia
Nov 14 '18 at 13:08
Also, below pattern works for me, but i know for sure it's not the best solution.
^[^0-9`|~|!|@|#|$|%|^|&|*|(|)|+|=|[||||\|'|<|,|.|>|?|/|""|;|:|s]*[a-zA-Z0-9_]*[^`|~|!|@|#|$|%|^|&|*|(|)|+|=|[||||\|'|<|,|.|>|?|/|""|;|:|s]*[^0-9`|~|!|@|#|$|%|^|&|*|(|)|+|=|[||||\|'|<|,|.|>|?|/|""|;|:|s]$
– shubham deodia
Nov 14 '18 at 13:08
Try
/^(?=[A-Z])(?=.*[A-Z]$)(?!.*_d)(?!.*d_)w+$/i
– Wiktor Stribiżew
Nov 14 '18 at 14:04
Try
/^(?=[A-Z])(?=.*[A-Z]$)(?!.*_d)(?!.*d_)w+$/i
– Wiktor Stribiżew
Nov 14 '18 at 14:04
add a comment |
2 Answers
2
active
oldest
votes
What you might do is use negative lookaheads to assert your requirements:
^(?![0-9._])(?!.*[0-9._]$)(?!.*d_)(?!.*_d)[a-zA-Z0-9_]+$
Explanation
^
Assert the start of the string(?![0-9._])
Negative lookahead to assert that the string does not start with[0-9._]
(?!.*[0-9._]$)
Negative lookahead to assert that the string does not end with[0-9._]
(?!.*d_)
Negative lookahead to assert that the string does not contain a digit followed by an underscore(?!.*_d)
Negative lookahead to assert that the string does not contain an underscore followed by a digit[a-zA-Z0-9_]+
Match what is specified in the character class one or more times. You can add to the character class what you would allow to match, for example also add a.
$
Assert the end of the string
Regex demo
add a comment |
Your opening and closing sections; [^0-9._]
, say match ANY character other than those.
So you need to change it to be what you can match.
/^[A-Z][A-Z0-9_]*[A-Z]$/i
And since you now said one character is valid:
/^[A-Z]([A-Z0-9_]*[A-Z])?$/i
this works well, but there's a little problem, it requires at-least 2 characters to validate. I am wondering there a can solution which can work with single character.
– shubham deodia
Nov 14 '18 at 13:13
Well that was not in the original question....
– epascarello
Nov 14 '18 at 13:15
Well I did add it, this is simple pattern, doubt you need complexity of looking ahead.
– epascarello
Nov 14 '18 at 14:06
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53300922%2fregex-pattern-to-not-allow-special-character-except-underscore%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
What you might do is use negative lookaheads to assert your requirements:
^(?![0-9._])(?!.*[0-9._]$)(?!.*d_)(?!.*_d)[a-zA-Z0-9_]+$
Explanation
^
Assert the start of the string(?![0-9._])
Negative lookahead to assert that the string does not start with[0-9._]
(?!.*[0-9._]$)
Negative lookahead to assert that the string does not end with[0-9._]
(?!.*d_)
Negative lookahead to assert that the string does not contain a digit followed by an underscore(?!.*_d)
Negative lookahead to assert that the string does not contain an underscore followed by a digit[a-zA-Z0-9_]+
Match what is specified in the character class one or more times. You can add to the character class what you would allow to match, for example also add a.
$
Assert the end of the string
Regex demo
add a comment |
What you might do is use negative lookaheads to assert your requirements:
^(?![0-9._])(?!.*[0-9._]$)(?!.*d_)(?!.*_d)[a-zA-Z0-9_]+$
Explanation
^
Assert the start of the string(?![0-9._])
Negative lookahead to assert that the string does not start with[0-9._]
(?!.*[0-9._]$)
Negative lookahead to assert that the string does not end with[0-9._]
(?!.*d_)
Negative lookahead to assert that the string does not contain a digit followed by an underscore(?!.*_d)
Negative lookahead to assert that the string does not contain an underscore followed by a digit[a-zA-Z0-9_]+
Match what is specified in the character class one or more times. You can add to the character class what you would allow to match, for example also add a.
$
Assert the end of the string
Regex demo
add a comment |
What you might do is use negative lookaheads to assert your requirements:
^(?![0-9._])(?!.*[0-9._]$)(?!.*d_)(?!.*_d)[a-zA-Z0-9_]+$
Explanation
^
Assert the start of the string(?![0-9._])
Negative lookahead to assert that the string does not start with[0-9._]
(?!.*[0-9._]$)
Negative lookahead to assert that the string does not end with[0-9._]
(?!.*d_)
Negative lookahead to assert that the string does not contain a digit followed by an underscore(?!.*_d)
Negative lookahead to assert that the string does not contain an underscore followed by a digit[a-zA-Z0-9_]+
Match what is specified in the character class one or more times. You can add to the character class what you would allow to match, for example also add a.
$
Assert the end of the string
Regex demo
What you might do is use negative lookaheads to assert your requirements:
^(?![0-9._])(?!.*[0-9._]$)(?!.*d_)(?!.*_d)[a-zA-Z0-9_]+$
Explanation
^
Assert the start of the string(?![0-9._])
Negative lookahead to assert that the string does not start with[0-9._]
(?!.*[0-9._]$)
Negative lookahead to assert that the string does not end with[0-9._]
(?!.*d_)
Negative lookahead to assert that the string does not contain a digit followed by an underscore(?!.*_d)
Negative lookahead to assert that the string does not contain an underscore followed by a digit[a-zA-Z0-9_]+
Match what is specified in the character class one or more times. You can add to the character class what you would allow to match, for example also add a.
$
Assert the end of the string
Regex demo
edited Nov 14 '18 at 13:41
answered Nov 14 '18 at 13:13
The fourth birdThe fourth bird
23.9k81429
23.9k81429
add a comment |
add a comment |
Your opening and closing sections; [^0-9._]
, say match ANY character other than those.
So you need to change it to be what you can match.
/^[A-Z][A-Z0-9_]*[A-Z]$/i
And since you now said one character is valid:
/^[A-Z]([A-Z0-9_]*[A-Z])?$/i
this works well, but there's a little problem, it requires at-least 2 characters to validate. I am wondering there a can solution which can work with single character.
– shubham deodia
Nov 14 '18 at 13:13
Well that was not in the original question....
– epascarello
Nov 14 '18 at 13:15
Well I did add it, this is simple pattern, doubt you need complexity of looking ahead.
– epascarello
Nov 14 '18 at 14:06
add a comment |
Your opening and closing sections; [^0-9._]
, say match ANY character other than those.
So you need to change it to be what you can match.
/^[A-Z][A-Z0-9_]*[A-Z]$/i
And since you now said one character is valid:
/^[A-Z]([A-Z0-9_]*[A-Z])?$/i
this works well, but there's a little problem, it requires at-least 2 characters to validate. I am wondering there a can solution which can work with single character.
– shubham deodia
Nov 14 '18 at 13:13
Well that was not in the original question....
– epascarello
Nov 14 '18 at 13:15
Well I did add it, this is simple pattern, doubt you need complexity of looking ahead.
– epascarello
Nov 14 '18 at 14:06
add a comment |
Your opening and closing sections; [^0-9._]
, say match ANY character other than those.
So you need to change it to be what you can match.
/^[A-Z][A-Z0-9_]*[A-Z]$/i
And since you now said one character is valid:
/^[A-Z]([A-Z0-9_]*[A-Z])?$/i
Your opening and closing sections; [^0-9._]
, say match ANY character other than those.
So you need to change it to be what you can match.
/^[A-Z][A-Z0-9_]*[A-Z]$/i
And since you now said one character is valid:
/^[A-Z]([A-Z0-9_]*[A-Z])?$/i
edited Nov 14 '18 at 13:17
answered Nov 14 '18 at 13:11
epascarelloepascarello
154k14136185
154k14136185
this works well, but there's a little problem, it requires at-least 2 characters to validate. I am wondering there a can solution which can work with single character.
– shubham deodia
Nov 14 '18 at 13:13
Well that was not in the original question....
– epascarello
Nov 14 '18 at 13:15
Well I did add it, this is simple pattern, doubt you need complexity of looking ahead.
– epascarello
Nov 14 '18 at 14:06
add a comment |
this works well, but there's a little problem, it requires at-least 2 characters to validate. I am wondering there a can solution which can work with single character.
– shubham deodia
Nov 14 '18 at 13:13
Well that was not in the original question....
– epascarello
Nov 14 '18 at 13:15
Well I did add it, this is simple pattern, doubt you need complexity of looking ahead.
– epascarello
Nov 14 '18 at 14:06
this works well, but there's a little problem, it requires at-least 2 characters to validate. I am wondering there a can solution which can work with single character.
– shubham deodia
Nov 14 '18 at 13:13
this works well, but there's a little problem, it requires at-least 2 characters to validate. I am wondering there a can solution which can work with single character.
– shubham deodia
Nov 14 '18 at 13:13
Well that was not in the original question....
– epascarello
Nov 14 '18 at 13:15
Well that was not in the original question....
– epascarello
Nov 14 '18 at 13:15
Well I did add it, this is simple pattern, doubt you need complexity of looking ahead.
– epascarello
Nov 14 '18 at 14:06
Well I did add it, this is simple pattern, doubt you need complexity of looking ahead.
– epascarello
Nov 14 '18 at 14:06
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53300922%2fregex-pattern-to-not-allow-special-character-except-underscore%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Also, below pattern works for me, but i know for sure it's not the best solution.
^[^0-9`|~|!|@|#|$|%|^|&|*|(|)|+|=|[||||\|'|<|,|.|>|?|/|""|;|:|s]*[a-zA-Z0-9_]*[^`|~|!|@|#|$|%|^|&|*|(|)|+|=|[||||\|'|<|,|.|>|?|/|""|;|:|s]*[^0-9`|~|!|@|#|$|%|^|&|*|(|)|+|=|[||||\|'|<|,|.|>|?|/|""|;|:|s]$
– shubham deodia
Nov 14 '18 at 13:08
Try
/^(?=[A-Z])(?=.*[A-Z]$)(?!.*_d)(?!.*d_)w+$/i
– Wiktor Stribiżew
Nov 14 '18 at 14:04