I am trying to allow alphanumeric and some special characters
var regx = /^[A-Za-z0-9._-\] ]+$/;
I tried escaping the ] sign with the forward slash but it still doesnt work.
What am I missing
You also need to escape the - character:
/^[A-Za-z0-9._\-\] ]+$/
//------------^
Escaping - is not always necessary. Here, however, it is used inside square brackets which makes the JavaScript engine assume that you are trying to specify the range from _-] which causes a "Range out of order in character class" error.
Note that /[_-a]/ is valid regex and matches characters _, ` and a (ASCII codes 95...97); which may not be the desired outcome.
If you try your regex on an online regex tester like regex101 you'd get the error:
Regex link
You have to escape - using \-:
^[A-Za-z0-9._\-\] ]+$
Btw, you can shorten your regex to:
^[\w.\-% ]+$
Edit: added regex for your comment:
^[\w.-\]\[ #$>()#{}'"]+$
Working demo
Related
i am working in regex my regex is /\[([^]\s]+).([^]]+)\]/g this works great in PHP for [http://sdgdssd.com fghdfhdhhd]
but when i use this regex for javascript it do not match with this input string
my input is [http://sdgdssd.com fghdfhdhhd]
In JavaScript regex, you must always escape the ] inside a character class:
\[([^\]\s]+).([^\]]+)\]
See the regex demo
JS parsed [^] as *any character including a newline in your regex, and the final character class ] symbol as a literal ].
In this regard, JS regex engine deviates from the POSIX standard where smart placement is used to match [ and ] symbols with bracketed expressions like [^][].
The ] character is treated as a literal character if it is the first character after ^: [^]abc].
In JS and Ruby, that is not working like that:
You can include an unescaped closing bracket by placing it right after the opening bracket, or right after the negating caret. []x] matches a closing bracket or an x. [^]x] matches any character that is not a closing bracket or an x. This does not work in JavaScript, which treats [] as an empty character class that always fails to match, and [^] as a negated empty character class that matches any single character. Ruby treats empty character classes as an error. So both JavaScript and Ruby require closing brackets to be escaped with a backslash to include them as literals in a character class.
Related:
(?1) regex subroutine used to shorten a PCRE pattern conversion - REGEX from PHP to JS
I would like to add this little fact about translating PHP preg_replace Regex in JavaScript .replace Regex :
<?php preg_replace("/([^0-9\,\.\-])/i";"";"-1 220 025.47 $"); ?>
Result : "-1220025.47"
with PHP, you have to use the quotes "..." around the Regex, a point comma to separate the Regex with the replacement and the brackets are used as a repetition research (witch do not mean the same thing at all.
<script>"-1 220 025.47 $".replace(/[^0-9\,\.\-]/ig,"") </script>
Result : "-1220025.47"
With JavaScript, no quotes around the Regex, a comma to separate Regex with the replacement and you have to use /g option in order to say multiple research in addition of the /i option (that's why /ig).
I hope this will be usefull to someone !
Note that the "\," may be suppressed in case of "1,000.00 $" (English ?) kind of number :
<script>"-1,220,025.47 $".replace(/[^0-9\.\-]/ig,"")</script>
<?php preg_replace("/([^0-9\.\-])/i";"";"-1,220,025.47 $"); ?>
Result : "-1220025.47"
Using Jquery validator plugin in my implementation. Need a regular expression which excludes special characters like , and &.
is there any regular expression for this. also if this special characters are anywhere in the string it should find and throw the error.
You can use regular expressions like this:
[\,\&]
you can add as much as u want to this.
try it out yourself on this site:
http://www.regexr.com/
/[,&]/g
matches , and &.
Demo: https://regex101.com/r/gY0mC3/2#javascript
If you want to search for every special character except letters, numbers and the underscore, use
/\W/g
Demo: https://regex101.com/r/gY0mC3/5#javascript
If you need to include spaces (e.g. a name) use
/[^\w\s]/g
Demo: https://regex101.com/r/gY0mC3/4#javascript
The brackets [] define custom regex classes.
To match a character for only those characters, you can do [\,\&].
To match all except that, you can add a ^, such as [^\,\&].
To match any non-word character, you can use \W (any character not a-z, A-Z, 0-9, or _).
To include an underscore, you can do [\W_].
Keep in mind that whitespaces are represented by \s and that depending on your environment, you may need to escape (add an additional backslash to) your backslashes.
I saw the other posts but none of them help me ...
So, i tried to match url in a string in javascript with regex it works perfectly on regex101 but fails in javascript.
var matches = feed.content.match(
'/((http|https|ftp):\/\/([a-zA-Z0-9\.\-\_\%]+\/?){1}([a-zA-Z0-9\.\-\_]+\/?)*(\?[a-zA-Z0-9\.\-\_\%\+\=\&\:]*)*)/ig'
);
And firebug returns me
SyntaxError: invalid quantifier
Please can you help me ?
As pointed out in the comments, you should remove the single quotes enclosing the regex. As well as that, I would propose making a few changes to the expression itself:
((https?|ftp):\/\/([\w.%-]+\/?)([\w.-]+\/?)*(\?[\w.%+=&:-]*)*)
The ? after the smeans that it is optional, so http and https will both match. \w is the word character class, so that covers A-Za-z0-9_ much more concisely. There's no need to escape all the symbols but a useful trick is to put the - at the end of the character class, so that it isn't interpreted as a range between two characters. The {1} isn't necessary as that's the default behaviour.
updated on regex101
You're passing the regex as a string - just get rid of the outer quotes.
var matches = feed.content.match(
/((http|https|ftp):\/\/([a-zA-Z0-9\.\-\_\%]+\/?){1}([a-zA-Z0-9\.\-\_]+\/?)*(\?[a-zA-Z0-9\.\-\_\%\+\=\&\:]*)*)/ig
);
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).
using http://www.regular-expressions.info/javascriptexample.html I tested the following regex
^\\{1}([0-9])+
this is designed to match a backslash and then a number.
It works there
If I then try this directly in code
var reg = /^\\{1}([0-9])+/;
reg.exec("/123")
I get no matches!
What am I doing wrong?
Update:
Regarding the update of your question. Then the regex has to be:
var reg = /^\/(\d+)/;
You have to escape the slash inside the regex with \/.
The backslash needs to be escaped in the string too:
reg.exec("\\123")
Otherwise \1 will be treated as special character.
Btw, the regular expression can be simplified:
var reg = /^\\(\d+)/;
Note that I moved the quantifier + inside the capture group, otherwise it will only capture a single digit (namely 3) and not the whole number 123.
You need to escape the backslash in your string:
"\\123"
Also, for various implementation bugs, you may want to set reg.lastIndex = 0;.
In addition, {1} is completely redundant, you can simplify your regex to /^\\(\d)+/.
One last note: (\d)+ will only capture the last digit, you may want (\d+).