How to specify url from array with regex in js [duplicate] - javascript

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

Related

How to validate composed strings with regex [duplicate]

This question already has answers here:
Check whether a string matches a regex in JS
(13 answers)
Closed 4 months ago.
I want to validate/write a regex of this form: uuid OR uuid-cust-uuid
It should ONLY return true when I test with a valid uuid OR a composed uuid like uuid-cust-uuid
Here is the regex I have written thus far:
const uuid =
/[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}(\-cust\-[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12})?/;
You can use the test method and it will do the job.
const regexUUID = /[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}(\-cust\-[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12})?/;
const uuid = 'ec3bf3c6-85be-4169-971c-0c49be945b51';
console.log(regexUUID.test(uuid));
Edited:
As for your requirement, you can try something like this:
// Wrtie base uuid regex
const baseUUIDRegex = '[0-9a-fA-F]{8}-([0-9a-fA-F]{4}-){3}[0-9a-fA-F]{12}';
// Then use it inside another regex like this
const customRegex = `^${baseUUIDRegex}-cust-${baseUUIDRegex}$`;
console.log('ec3bf3c6-85be-4169-971c-0c49be945b51-cust-ec3bf3c6-85be-4169-971c-0c49be945b51'.match(customRegex));
Hope it will be helpful for you.

How to include variable in Regex [duplicate]

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

JavaScript Regex Question. How to split this string [duplicate]

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 2 years ago.
I'm new to Regex and was wondering how to split this string s4://test-dev/prefixes/file.mxf into ['test-dev', 'prefixes/file.mxf']. I want it to work for an unknown file path.
Ex)
s4://test-dev/prefixes/file.mxf/newpath/anothernewpath into
['test-dev', 'prefixes/file.mxf/newpath/anothernewpath']
If you are using regex for splitting (which is not necessary) you can use this regex: s4:\/\/test-dev\/(.*) and use first group (first parentheses) as second string (first one is always same as i can see), but easiest way is to find position of third '/' with this var pos=str.split("/", i).join("/").length; and then find substring from that position to end: var res = str.substring(pos, str.length-pos);
Instead of splitting, use capture groups.
url.match(/^[^/]+:\/\/([^/]+)\/?(.*)/).slice(1)
Please check the code below:
let url = "s4://test-dev/prefixes/file.mxf/newpath/anothernewpath"
let match = url.match(/^s4:\/\/([\w-]+)\/(.+)$/)
let result = [match[1], match[2]]
console.log(result)
The result is:
[
"test-dev",
"prefixes/file.mxf/newpath/anothernewpath"
]
/^[\w+\:\/\/]+[-w+]/i
matches 'text-dev'
/w+\:\/\/[-w+]\/gi/
matches the rest

Remove from string using JS [duplicate]

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 3 years ago.
I have a string with a list of filenames such as
var string = '1.jpg,2.jpg,3.png,4.jpg,5.webp'
Is there a way to remove everything that doesn't end in .jpg so the output would look like this:
var newstring = '1.jpg,2.jpg,4.jpg'
You may write something like this
string
.split(",")
.filter(value => value.endsWith(".jpg"))
.join(",")
Did you experiment with possible regular expressions you could use? You might be able to find the answer yourself thanks to this page from the Mozilla Developer Network: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
If your string is always a comma separated list, then split the string on commas, which will give you an array of items. Then splice the array and remove items that contain the .jpg pattern.
var string = '1.jpg,2.jpg,3.png,4.jpg,5.webp';
string.split(',').filter((name)=> name.includes('.jpg')).join(',');
//"1.jpg,2.jpg,4.jpg"
var string = '1.jpg,2.jpg,3.png,4.jpg,5.webp';
var stringArray=string.split(',');
newArray=[];
stringArray.forEach(element => {
if(element.indexOf('.jpg')>-1){ newArray.push(element)}
});
console.log("jpg Array :"+newArray)// output : jpg Array :1.jpg,2.jpg,4.jpg

RegEx: Extract GET variable from URL [duplicate]

This question already has answers here:
How can I get query string values in JavaScript?
(73 answers)
Closed 9 years ago.
RegExp gurus, heed my call!
This is probably super simple, but I've painted myself in a mental corner.
Taking a regular URL, split after the ?, which gives a string like variable=val&interesting=something&notinteresting=somethingelse I want to extract the value of interesting.
The name of the variable I'm interested in can be a substring of another variable.
So the match should be
either beginning of string or "&" character
followed by "interesting="
followed by the string I want to capture
followed by either another "&" or end of string
I tried something along the lines of
[\^&]interesting=(.*)[&$]
but I got nothing...
Update
This is to be run in a Firefox addon on every get request, meaning that jQuery is not available and if possible I would like to avoid the extra string manipulation caused by writing a function.
To me this feels like a generic "extract part of a string with regex" but maybe I'm wrong (RegEx clearly isn't my strong side)
simple solution
var arr = "variable=val&interesting=something&notinteresting=somethingelse".split("&");
for(i in arr) {
var splits = arr[i].split("=");
if(splits[0]=="interesting") alert(splits[1]);
}
also single line match
"variable=val&interesting=something&notinteresting=somethingelse".match(/(?:[&]|^)interesting=((?:[^&]|$)+)/)[1]
function getValue(query)
{
var obj=location.search.slice(1),
array=obj.split('&'),
len=array.length;
for(var k=0;k<len;k++)
{
var elm=array[k].split('=');
if(elm[0]==query)return elm[1];
}
}
This function directly extract the query URL and return the corresponding value if present.
//usage
var get=getValue('interesting');
console.log(get);//something
If you're using the Add-on SDK for Firefox, you can use the url module:
https://addons.mozilla.org/en-US/developers/docs/sdk/latest/modules/sdk/url.html
This is much better than using regex.

Categories