This question already has answers here:
Regular expression which matches a pattern, or is an empty string
(5 answers)
Closed 2 years ago.
I have the following regext:
var regex = /^(http(s)?:\/\/.)?(www\.)?[-a-zA-Z0-9#:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9#:%_\+.~#?&//=]*)/g;
function test() {
alert(regex.test(document.getElementById("myinput").value));
}
I want to allow url or empty string. regex solution please
How do I allow empty in this case?
https://jsfiddle.net/6kptovwc/2/
Thanks
Add an alternation with empty string (I've simplified a bit your regex):
^((?:https?:\/\/)?(?:www\.)?[-a-zA-Z0-9#:%._\+~#=]{2,256}\.[a-z]{2,6}\b[-a-zA-Z0-9#:%_\+.~#?&\/=]*|)$
or
^((?:https?:\/\/)?(?:www\.)?[-\w#:%.+~#=]{2,256}\.[a-z]{2,6}\b[-\w#:%+.~#?&\/=]*|)$
Demo & explanation
Just add OR(||) condition
function test() {
const elm = document.getElementById("myinput")
alert(elm.value === '' || regex.test(elm.value));
}
^$|pattern
var regex = /^$|(http(s)?:\/\/.)?(www\.)?[-a-zA-Z0-9#:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9#:%_\+.~#?&//=]*)/g;
Related
This question already has an answer here:
Regex to find string between slashes
(1 answer)
Closed 4 years ago.
I have the following path:
/s/STRINGINEED/abcdef/
How should I structure my regex to match STRINGINEED as a result?
/s/ is a fixed path, so I would like to get any string between /s/ and the following /.
To get the string coming after /s/ in the path, you can use the following regex:
\/s\/([\w]+)\/
Demo:
const regex = /\/s\/([\w\d]+)\//gm;
const str = `/s/STRINGINEED/abcdef/`;
console.log(regex.exec(str)[1]);
Another option is to use .split() method:
str.split('/s/')[1].split("/")[0]
Demo:
var str = '/s/STRINGINEED/abcdef/';
console.log(str.split('/s/')[1].split("/")[0]);
You might execute:
\/s\/([^\/]*)
and then use the first group matched.
Demo: https://regex101.com/r/MaGIlD/2
This question already has answers here:
How to tell if a string contains a certain character in JavaScript?
(21 answers)
Closed 4 years ago.
I'm a beginner in node.js so please do excuse me if my question is foolish. As we know we can use
var regex = /[ !##$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]/g;
regex.test(str);
to check whether a string contains special charecters or not .But what I'm asking is how to check for only a particular charecter means how can I check only presence of #.
I tried to do
var regex = /[#]/g; regex.test(str).
Although it's not working but are there any other method of doing this?
You don't need a regex to find a single character in a string. You can use indexOf, like this:
var hasHashtag = str.indexOf('#') >= 0;
This returns true if the character is in the string.
Use includes to check the existence of # in your string. You don't actually require regex to do that.
var str = 'someSt#ring';
var res = str.includes('#');
console.log(res);
str = 'someSt#ri#ng';
res = str.includes('#');
console.log(res);
str = 'someString';
res = str.includes('#');
console.log(res);
Use indexOf
str.indexOf('#') >= 0;
This question already has answers here:
How can I remove a character from a string using JavaScript?
(22 answers)
Closed 5 years ago.
I'm looking on how to remove a char from a string for example let's say i have "#22UP0G0YU" i want it to remove the # from it how would i do? I also have a small little other question too about how to make string upper case as well thanks in advance.
To remove a specific char I normally use replace, also good for a set of chars:
var str = '#22UP0G0YU';
var newString = str.replace('#', ''); // result: '22UP0G0YU'
To Uppercase, just use .toUpperCase();
var str = '#22UP0G0yu';
var newString = str.replace('#', '').toUpperCase(); // result: '22UP0G0YU'
This question already has answers here:
How do you access the matched groups in a JavaScript regular expression?
(23 answers)
Closed 5 years ago.
now I begin learning regex, and I have set of string in format like "(9/13)", and I need get second number. I try this regex: /\(.*?[^\d]*(\d+?)\)/g, in online regex it works normally.
But here:
var d = "(9/13)";
var v = /\(.*?[^\d]*(\d+?)\)/g;
alert(d.match(v));
it returns "(9/13)" , what am I doing wrong?
const source = "(9/13)";
const re = /\/(\d+)\)/;
console.log('result', re.exec(source).pop())
you can use Regex.exec() to find the number
const source = "(9/13)";
const re = /\(\d+\/(\d+)\)/;
console.log(re.exec(source)[1])
This question already has answers here:
Find and get only number in string
(4 answers)
Closed 8 years ago.
I have the following string
/Date(1317772800000)/
I want to use a Javascript regular expression to extract the numerical portion of it
1317772800000
How is this possible?
That should be it
var numPart = "/Date(1317772800000)/".match(/(\d+)/)[1];
No need for regex. Use .substring() function. In this case try:
var whatever = "/Date(1317772800000)/";
whatever = whatever.substring(6,whatever.length-2);
This'll do it for you: http://regex101.com/r/zR0wH4
var re = /\/Date\((\d{13})\)\//;
re.exec('/Date(1317772800000)/');
=> ["/Date(1317772800000)/", "1317772800000"]
If you don't care about matching the date portion of the string and just want extract the digits from any string, you can use this instead:
var re = /(\d+)/;
re.exec('/Date(1317772800000)/')
["1317772800000", "1317772800000"]