I am facing an issue with a regular expression while trying to block any string which has minus(-) in the beginning of some white listed characters.
^(?!-.*$).([a-zA-Z0-9-:#\\,()\\/\\.]+)$
It is blocking minus(-) at place and allowing it any where in the character sequence but this regex is not working if the passed string is single character.
For e.g A or 9 etc.
Please help me out with this or give me a good regex to do the task.
Your pattern requires at least 2 chars in the input string because there is a dot after the first lookahead and then a character class follows that has + after it (that is, at least 1 occurrence must be present in the string).
So, you need to remove the dot. Also, you do not need to escape any special char inside a character class. Besides, to avoid matching strings atarting with - a mere (?!-) will suffice, no need adding .*$ there. You may use
^(?!-)[a-zA-Z0-9:#,()/.-]+$
See the regex demo. Remember to escape / if used in a regex literal notation in JavaScript, there is no need to escape it in a constructor notation or in a Java regex pattern.
Details
^ - start of a string
(?!-) - cannot start with -
[a-zA-Z0-9:#,()/.-]+ - 1 or more ASCII letters, digits and special chars defined in the character class (:, #, ,, (, ), /, ., -)
$ - end of string.
If i understand correctly, and you don't want a minus at the beginning, does ^[^-].* work as a regex for you? Java's "matches" would return false if it starts with minus
There is a method in a String class that provides you exactly what you are asking for - it's a startsWith() method - you could use this method in your code like this (you can translate it as "If the given String doesn't start with -, doSomething, in other case do the else part, that can contain some code or might be empty if you want nothing to be done if the given String starts with - ") :
if(!(yourString.startsWith("-"))) {
doSomething()
} else {
doNothingOrProvideAnyInformationAboutWrongInput()
}
I think that it can help you.
^(?!-).*[a-zA-Z0-9-:#\\,()\/\\.]+$
Related
I'm altering the segment of a string in javascript and got the following working (first iteration -optional comma).
var foo = "wat:a,username:x,super:man"
foo.replace(/(username\:\w+)(?:,)*/,"go:home,");
//"wat:a,go:home,super:man"
The trick now is that I might actually replace a key/value with only the key ... so I need to capture the original group with both optional value + optional comma.
var foo = "wat:a,username:,super:man"
foo.replace(/ ????? /,"go:home,");
//"wat:a,go:home,super:man"
As a bonus I'd like the most concise way to capture both optional numbers/and letters (updating my original to also support)
var foo = "wat:a,username:999,super:man"
foo.replace(/ ????? /,"go:home,");
//"wat:a,go:home,super:man"
You need to replace the + (1 or more occurrences of the preceding subpattern) quantifier with the * (0 or more occurrences of the preceding subpattern).
See Quantifier Cheatsheet at rexegg.com:
A+ One or more As, as many as possible (greedy), giving up characters if the engine needs to backtrack (docile)
A* Zero or more As, as many as possible (greedy), giving up characters if the engine needs to backtrack (docile)
Besides, you are not using any of the capturing groups defined in the pattern, so I suggest removing them.
.replace(/username:\w*,*/,"go:home,")
^
And if you have just 1 optional ,, use just the ? quantifier (1 or 0 repetition of the preceding subpattern):
.replace(/username:\w*,?/,"go:home,")
^
Note that in case you can have any characters before the end of string or comma, you can also use Fede's suggestion of using a negated character class: /username:[^,]*,*/. The [^,]* matches any character (even a newline) other than a comma.
Also, please note that you do not need to escape a colon. The characters that must be escaped outside of character class to be treated as literals are ., *, +, ?, ^, $, {, (, ), |, [, ], \. See RegExp MDN reference.
I'm not sure if I understood your question, but if you want to match username:?? you can use below regex:
(username\:\w*)
Working demo
Update: As stribizhev, pointed in his comment \w* can do the trick, however if you want to extend the regex to any characters besides letters or numbers you can use:
(username\:[^,]*)
Tried to search for /\,$/ online, but coudnt find anything.
I have:
coords = coords.replace(/\,$/, "");
Im guessing it returns coords string index number. What I have to search online for this, so I can learn more?
/\,$/ finds the comma character (,) at the end of a string (denoted by the $) and replaces it with empty (""). You sometimes see this in regex code aiming to clean up excerpts of text.
It's a regular expression to remove a trailing comma.
That thing is a Regular Expression, also known as regex or regexp. It is a way to "match" strings using some rules. If you want to learn how to use it in JavaScript, read the Mozilla Developer Network page about RegExp.
By the way, regular expressions are also available on most languages and in some tools. It is a very useful thing to learn.
That's a regular expression that finds a comma at the end of a string. That code removes the comma.
// defines a JavaScript regular expression, used to match a pattern within a string.
\,$ is the pattern
In this case \, translates to ,. A backslash is used to escape special characters, but in this case, it's not necessary. An example where it would be necessary would be to remove trailing periods. If you tried to do that with /.$/ the period here has a different meaning; it is used as a wildcard to match [almost] any character (aside for some newlines). So in this case to match on "." (period character) you would have to escape the wildcard (/\.$/).
When $ is placed at the end of the pattern, it means only look at the end of the string. This means that you can't mistakingly find a comma anywhere in the middle of the string (e.g., not after help in help, me,), only at the end (trailing). It also speeds of the regular expression search considerably. If you wanted to match on characters only at the beginning of the string, you would start off the pattern with a carat (^), for instance /^,/ would find a comma at the start of a string if one existed.
It's also important to note that you're only removing one comma, whereas if you use the plus (+) after the comma, you'd be replacing one or more: /,+$/.
Without the +; trailing commas,, becomes trailing commas,
With the +; no trailing comma,, becomes no trailing comma
I'm writing a function that takes a prospective filename and validates it in order to ensure that no system disallowed characters are in the filename. These are the disallowed characters: / \ | * ? " < >
I could obviously just use string.indexOf() to search for each special char one by one, but that's a lot longer than it would be to just use string.search() using a regular expression to find any of those characters in the filename.
The problem is that most of these characters are considered to be part of describing a regular expression, so I'm unsure how to include those characters as actually being part of the regex itself. For example, the / character in a Javascript regex tells Javascript that it is the beginning or end of the regex. How would one write a JS regex that functionally behaves like so: filename.search(\ OR / OR | OR * OR ? OR " OR < OR >)
Put your stuff in a character class like so:
[/\\|*?"<>]
You're gonna have to escape the backslash, but the other characters lose their special meaning. Also, RegExp's test() method is more appropriate than String.search in this case.
filenameIsInvalid = /[/\\|*?"<>]/.test(filename);
Include a backslash before the special characters [\^$.|?*+(){}, for instance, like \$
You can also search for a character by specified ASCII/ANSI value. Use \xFF where FF are 2 hexadecimal digits. Here is a hex table reference. http://www.asciitable.com/ Here is a regex reference http://www.regular-expressions.info/reference.html
The correct syntax of the regex is:
/^[^\/\\|\*\?"<>]+$/
The [^ will match anything, but anything that is matched in the [^] group will return the match as null. So to check for validation is to match against null.
Demo: jsFiddle.
Demo #2: Comparing against null.
The first string is valid; the second is invalid, hence null.
But obviously, you need to escape regex characters that are used in the matching. To escape a character that is used for regex needs to have a backslash before the character, e.g. \*, \/, \$, \?.
You'll need to escape the special characters. In javascript this is done by using the \ (backslash) character.
I'd recommend however using something like xregexp which will handle the escaping for you if you wish to match a string literal (something that is lacking in javascript's native regex support).
replace(/[^0-9]/g,''));
Replace is a method
What does / indicate?
What does ^ indicate along with 0-9
What does /g indicate?
Do we need to start a regular expression with / or can we start with anything?
The / introduces a regular expression literal (just like " and ' introduce string literals). A regular expression literal is in the form /expression/flags, where expression is the body of the expression, and flags are optional flags (i for case-insensitive, g for global, m for multi-line stuff).
The ^ as the first character within [] means any character not matching the following. So [^0-9] means "any character except 0 through 9".
The /g ends the regular expression literal and includes the "global" flag on it. Without the g, replace would only replace the first match, not all of them.
In all, what that does is replace any character that isn't 0 through 9 with a blank — e.g., removes non-digits. It could be written more simply as:
var result = str.replace(/\D/g, '');
...because \D (note that's an upper-case D) means "non-digit".
MDC has a decent page on regular expressions.
The / and / are the start and end of the regex pattern, the g mean global (anything after the 2nd / is an optional modifier for the regex).
^ means not.
So in this case it'll remove any character that isn't a number.
See the manual for replace
See regular expression literals
See using special characters
See searching with flags
replace is method of string type
/ / indicates there's a regular expression inside of them
^ inside [] means "not"
"g" means to replace globally
regular expressions in javascript should put in to a pair of "/"
This W3 Schools tutorial should cover most of the basics. This other tutorial covers the flasg, such as /g which can be passed to the regex engine.
yes
start and end of regex
not, that just basically means, match any non-integer
global replacement, the effect of not having that is replacement only done for the first encounter.
At least in javascript, yes you have to use /.
/ indicates the beginning and end of the regexp. Hence in your case [^0-9] is the regex.
^ indicates the start of line
/g indicates the substitution to take place for all the match - globl, and not only for the first match.
/g enables "global" matching. When using the replace() method, specify this modifier to replace all matches, rather than only the first one.
/ start regex
^ match except the symbols 0-9
Well, as to creating one, this forum is not the best for that -- it is a rather large question, best left to one of the best resources on RegExp that I know of.
It looks like you're in JS, so:
replace is a method of String. It replaces the provided expression with the second string, in this case nothing.
In JavaScript / must begin and end all RegEx's, all / in the middle must be escaped with a \ (so they look like this: \/). In other languages (PHP, Perl being some of the most prominent), you can use other characters such as ~ and #.
^ inside of [] means negation, - means range, so [^0-9] means "not 0, 1, 2, 3, 4, 5, 6, 7, 8 or 9" [0-9] does have a shorthand of \d. So /[^\d]/g is a valid, alternate way to say the same thing.
/g means "global" as in "match all incidents, not just the first.
Your expression means, "replace all non-digits with nothing".
The / encapsulate your pattern (you need to escape / with \ if you want to use it in pattern)
and the trailing character after the slashes are modifiers. 'g' in this case means global search (i.e. find all matches)
^ is negation.. [0-9] is range indicating all numbers from 0 to 9.
so [^0-9] means anything except numbers
So This regex basically replaces anything except numbers in the string with '' (i.e. remove them)
Regex has lots of other features, you should research them!
What it does: Removes all non-numeric (0-9) characters.
The forward slash (/) is used when you declare RegExp literals
The [^0-9] means any character OTHER THAN 0-9. The ^ means "other than". You can remove it and it'll look for only a character 0-9.
The /g represents global replacement.
So this will look for any non-number character and replace it with nothing.
As Shamim notes, regular-expressions.info/is a great site. Best of luck!
You can try out javascript regex's on this site: http://regexpal.com/
Couples with http://www.regular-expressions.info/tutorial, it's a great resource for learning.
We have been using the following js/regex to find and replace all non-alphanumeric characters apart from - and +
outputString = outputString.replace(/[^\w|^\+|^-]*/g, "");
However it doesn't work entirely - it doesn't replace the ^ and | characters. I can't help but wonder if this is something to do with the ^ and | being used as meta-characters in the regex itself.
I've tried switching to use [\W|^+|^-], but that replaces the - and +. I thought that possibly a lookahead assertion may be the answer, but I'm not very sure how to implement them.
Has anyone got an idea how to accomplish this?
Character classes do not do alternation, hence why the | is literal, and the ^ must be at the start of the class to take effect (otherwise it's treated literally.)
Use this:
[^\w+-]+
(Also, if - is not last, it needs to be escaped as \- inside a character class - so be careful if more characters might be added to the exception list).
You could also do it with a negative lookahead like this:
(?![+-])\W
Note: You do not want a * or + after that \W, since the lookahead only applies to the immediately following character (and the g flag makes the replace repeat until done).
Also note that \w and \W consider _ as a word character. If that's not desired then to replace that you can use (?![+-])[\W_] (or use explicit ranges in the first expressions).