This question already has answers here:
Negate characters in Regular Expression [closed]
(4 answers)
Closed 9 months ago.
I need to prevent user from entering the following set of chars:
~ " # % & * : < > ? / \ { | } .
Important is that, anything else will be allowed, but only the characters shown above will be forbidden.
Currently, I'm using the following regex but this does also forbid for example ! sign.
private static folderRegex = /^[A-Za-z0-9_-]+[ßüÜöÖäÄ\w]*$/;
Just put the set of characters in an inverse set and add a ^ and a $ to stretch the pattern to the full length of the input.
private static folderRegex = /^[^~"#%&*:<>?\/\\{|}]+$/;
Related
This question already has answers here:
What does this symbol mean in JavaScript?
(1 answer)
Meaning of javascript text between two slashes
(3 answers)
Closed 2 years ago.
I'm referring to a book and it has code I can't understand:
http.createServer(function(req,res){
// normalize url by removing querystring, optional
// trailing slash, and making lowercase
var path = req.url.replace(/\/?(?:\?.*)?$/, '').toLowerCase();
}
I have a problem in the following line:
var path = req.url.replace(/\/?(?:\?.*)?$/, '').toLowerCase();
What is that first argument of replace method?
The first argument is the pattern, a regex on what to look for, and the second argument is to what to replace the instances with. In this case, \/? is the / character with zero or one instance of it, and (?:\?.*)? not to capture the piece where it has ? zero to unlimited times.
This question already has answers here:
How to escape regular expression special characters using javascript? [duplicate]
(3 answers)
Closed 2 years ago.
Sorry, this sounds very basic, but I really can't find on Google.
In order to replace contents in a string globally, you can use things like...
a.replace(/blue/g,'red')
But sometimes you need to replace characters that's not compatible with the example above, for example, the character ")"
So, this will fail...
const a ="Test(123)"
a = a.replace(/(/g, '')
VM326:1 Uncaught SyntaxError: Invalid regular expression: /(/: Unterminated group
How to replace string of characters like that ?
at :1:7
The special regular expression characters are:
. \ + * ? [ ^ ] $ ( ) { } = ! < > | : -
const a ="Test(123)";
console.log(a.replace(/\(/g, ''));
you need to use the escape char \ for this. there are set of char which need to be escaped in this replace(regex)
a.replace(/\(/g, '');
Find a full details here at MDN
you need to escape the ( with \ in a new variable because a is const
and it will work
var b = a.replace(/\(/g, '');
for more practicing use this site
regExr
This question already has answers here:
Regular Expressions: Is there an AND operator?
(14 answers)
Closed 7 years ago.
I have a simple filter function in my javascript, based on an input box.
function filter(selector, query) {
query = $.trim(query); //trim white space
query = query.replace(/ /gi, '|'); //add OR for regex
$(selector).each(function() {
($(this).text().search(new RegExp(query, "i")) < 0) ? (do something here)
So, if I have a table with a list of words, eg.
Alpha Centauri,
Beta Carbonate,
Charly Brown,
...
and I enter 'alpha cen' into my input box, the function searches for ('alpha' or 'cen') and returns the one record as desired.
However, if I replace the '|' with a '&' to search for ('alpha' and 'cen') I get no results. Even if I enter 'alpha centauri', I get no result at all.
Why?
While a | in a regex allows alternation, an & carries no special meaning. Your current code is trying to find a literal match for alpha&cen, which clearly doesn't match any of your data.
This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 2 years ago.
I need javascript regular expression to check string starting with U or C. Also it should be of length 10.
I tried this ^U|C{9}$ but not getting proper results.
Use this regex instead:
^[UC].{9}$
The . will match any character, which you missed out.
You should group the tokens when using | operator, so as to get intended results. Also, you must use .(any character except new-line) and make sure it is 9 characters long.
You can also use character class as in /^[UC].{9}$/
/^(U|C).{9}$/
You can also use simple javascript to do this
var chr = str.charAt(0);
if((chr == "U" || chr == "C") && str.length == 10){
// valid
}
This question already has answers here:
Is there a RegExp.escape function in JavaScript?
(18 answers)
Closed 4 years ago.
I'm trying to create a dynamic regex to select URL's based on a segment or the whole URL.
For example, I need to get var.match(/http:\/\/www.something.com\/something/)
The text inside the match() needs to be converted so that special characters have \ in front of them such for example "\/". I was not able to find a function that converts the URL to do this? Is there one?
If not, what characters require a \ in front?
I use this to escape a string when generating a dynamic regex:
var specials = /[*.+?|^$()\[\]{}\\]/g;
var url_re = RegExp(url.replace(specials, "\\$&"));
( ) [ ] ? * ^ $ \ . + | and in your case, / since you're using that as the delimiter in the match.
Further info mostly to pre-empt comments and downvotes: I don't really know where - come from as a special character. It's only special when inside character class brackets [ and ] which you're already escaping. If you want to include characters that are sometimes special (which the OP doesn't) that would include look-ahead/behind characters as well, which include =, < and >.