This question already has answers here:
Why do regex constructors need to be double escaped?
(5 answers)
Closed 1 year ago.
I want to use regex to allow/reject files during fileupload with a specific file type.
This is the working regex /(\.|\/)(xlsx)$/i expected output that I want to achieve.
What I'm trying is instead of hardcode the filetype, I want to pass a variable that holds filetype into the regex.
var fileType = 'xlsx';
var regexText = new RegExp("/(\.|\/)(" + fileType + ")$/i");
but the current output of regexText is /\/(.|\/)(xlsx)$\/i/ this is not same as expected output. How do I can get same output as mentioned above by using a variable without hardcode? Is there solution without using the RegExp that can give the same expected output?
try forming string for regex line before calling new RegExp
Related
This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 2 years ago.
How can detect url's from this string. Urls are in quotes that cause problem with multiple items.
"{"output":["www.google.com"],"screenshots":["http://10.200.200.14:5000/screenshots/id=215585/1.png","http://10.200.200.14:5000/screenshots/id=215585/2.png"]}"
I need to extract below:
http://10.244.224.44:5000/screenshots/id=215585/1.png
http://10.244.224.44:5000/screenshots/id=215585/2.png
Edit: I edited the line, it is actually a string and I need to extract urls to make them clickable in ace editor. So Ace editor wants me to specify a regex pattern.
const CustomHighlightRules = function CustomHighlightRules() {
let rules = new HighlightRules().getRules();
for (const rule in rules) {
rules[rule].unshift({
token: ['clickables'],
regex: /(((https?:\/\/)|(www\.))[^\s]+)/,
});
Actually you don't need Regex here:
var objJSON = JSON.parse('{"output":["www.google.com"],"screenshots":["http://10.200.200.14:5000/screenshots/id=215585/1.png","http://10.200.200.14:5000/screenshots/id=215585/2.png"]}');
Will parse the Json in obj and then:
document.write(obj.screenshots);
Use obj.screenshots
This question already has answers here:
Create RegExps on the fly using string variables
(6 answers)
Closed 4 years ago.
I have a regex when I instantiate the Regex object like this:
this.checkRegex = new RegExp(/^([0|\[+][0-9]{0,5})?([1-9][0-9]{0,15})$/);
It works fine, however If I store the regex in string it does not work:
private checkReg: string = '/^([0|\[+][0-9]{0,5})?([1-9][0-9]{0,15})$/';
this.checkRegex = new RegExp(this.checkReg);
I am using angular-typescript. What is the thing I am missing here when I am trying to instantiate by throwing string inside the constructor. Code sample will be really appreciated. Thanks for your help.
When you are passing a string to the RegExp constructor, you need to change it a little bit. Instead of
'/^([0|\[+][0-9]{0,5})?([1-9][0-9]{0,15})$/'
You would omit the preceding and trailing slash
'^([0|\\[+][0-9]{0,5})?([1-9][0-9]{0,15})$'
Note also double escaping the back slash.
If you want to store the RegExp as a String, store it without the forward slashes at the front and back. When creating the RegExp object, those get escaped:
new RegExp('/^([0|\[+][0-9]{0,5})?([1-9][0-9]{0,15})$/');
will result in
/\/^([0|[+][0-9]{0,5})?([1-9][0-9]{0,15})$\//
while,
new RegExp('^([0|\[+][0-9]{0,5})?([1-9][0-9]{0,15})$');
will work:
/^([0|[+][0-9]{0,5})?([1-9][0-9]{0,15})$/
This question already has answers here:
JavaScript RegExp objects
(5 answers)
Closed 6 years ago.
I am trying to validate if a string I entered matches the date format 'MM/yyyy'
Below is a sample of the code I am using for the same:
var date='05/2016'
var patt= new RegExp('^((0[1-9])|(1[0-2])|[1-9])\/(\d{4})$');
patt.test(date);
However the above code is returning false.
I tried running it with the regex checker:
https://regex101.com/
The pattern seems to be working fine.
Could someone please let me know what is missing.
https://jsfiddle.net/ymj6o8La/
You have to escape the string that is passed to RegExp (the backslashes).
var patt= new RegExp('^((0[1-9])|(1[0-2])|[1-9])\\/(\\d{4})$');
Even better, in your case, it's not dynamic, so you should use the literal RegExp instead
var patt = /^((0[1-9])|(1[0-2])|[1-9])\/(\d{4})$/
You should escape your backslashes. To represent \d or even \ you should another backslash behind it (e.g: \\) :
var date = '05/2016'
var patt = new RegExp('^((0[1-9])|(1[0-2])|[1-9])\\/(\\d{4})$');
console.log(patt.test(date));
Try using a pattern like this
patt= /^((0[1-9])|(1[0-2]))\/(\d{4})$/;
This question already has answers here:
Replace method doesn't work
(4 answers)
Closed 4 years ago.
I am trying to replace a single dash '-' character in a string with double dashes.
2015–09–01T16:00:00.000Z
to be
2015-–09-–01T16:00:00.000Z
This is the code I am using but it doesn't seem to be working:
var temp = '2015–09–01T16:00:00.000Z'
temp.replace(/-/g,'--')
In JavaScript Strings are immutable. So, when you modify a string, a new string object will be created with the modification.
In your case, the replace has replaced the characters but returns a new string. You need to store that in a variable to use it.
For example,
var temp = '2015–09–01T16:00:00.000Z';
temp = temp.replace(/–/g,'--');
Note The string which you have shown in the question, when copied, I realised that it is a different character but looks similar to – and it is not the same as hyphen (-). The character codes for those characters are as follows
console.log('–'.charCodeAt(0));
// 8211: en dash
console.log('-'.charCodeAt(0));
// 45: hyphen
The hyphen character – you have in the string is different from the one you have in the RegExp -. Even though they look alike, they are different characters.
The correct RegExp in this case is temp.replace(/–/g,'--')
Probably the easiest thing would be to just use split and join.
var temp = '2015–09–01T16:00:00.000Z'.split("-").join("--");
This question already has answers here:
Need a basename function in Javascript
(19 answers)
Closed 8 years ago.
I have a string like C:\Users\shail.jet\Desktop\cca-lan_test_cases_path.txt.
I want to get only file name that is in this case is cca-lan_test_cases_path.txt.I have tried it with javascript split function but it did'nt work. Any help will be appreciated.
"C:\\Users\\shail.jet\\Desktop\\cca-lan_test_cases_path.txt".split("\\").pop();
Make sure you add escape backslashes in the file path string otherwise javascript will ignore it.
That will split by backslash and then use pop to get the last element in the array which will be the file name.
var filename = fullPath.replace(/^.*[\\\/]/, '')
This will handle both \ OR / in paths
var filename = fullPath.replace(/^.*(\\|\/|\:)/, '');
should also protect from empty string