How can skip match with Regex i wanna to get ss and skip =
Code:
var patt = new RegExp("=ss","g");
var url = "https://mass.lass.com/?t=ss";
var x = url.match(patt);
console.log(x);
//result
=ss
i only need SS
Use a positive lookbehind. Instead of
new RegExp("=ss","g")
you can use
new RegExp("(?<==)ss", "g")
Try this function (more generic), it takes params and url as a parameters
const url = "https://mass.lass.com/?t=ss";
const getQueryParams = ( url, params ) => {
let reg = new RegExp( '[?&]' + params + '=([^&#]*)', 'i' );
let queryString = reg.exec(url);
return queryString ? queryString[1] : null;
};
console.log(getQueryParams(url, "t"))
Related
Let's say that this is my URL/Link that i have written in an input
https://www.instagram.com/p/CBt-W4jHZjH/
How can I get the "CBt-W4jHZjH" part?
var link = ?????
var a = link.val().trim();
var regex = new RegExp(/^(?!.*\.\.)(?!.*\.$)[^\W][\w.]{0,29}$/);
var validation = regex.test(a);
https://developer.mozilla.org/en-US/docs/Web/API/URL
const getLastPath = (url) => {
url = new URL(url);
const pathname = url.pathname;
const paths = pathname.split("/");
return paths.pop() || paths.pop();
}
console.log(getLastPath("https://www.instagram.com/p/CBt-W4jHZjH/")); // "CBt-W4jHZjH"
console.log(getLastPath("https://www.instagram.com/p/CBt-W4jHZjH")); // "CBt-W4jHZjH"
Many ways to do it. One way is to look for / any character but / ending with / end of line.
var url = 'https://www.instagram.com/p/CBt-W4jHZjH/'
var x = new URL(url);
console.log(x.pathname.match(/\/([^\/]+)\/?$/)[1])
Could be done with split. The filter removes the empty string caused by the trailing /.
var url = 'https://www.instagram.com/p/CBt-W4jHZjH/'
var x = new URL(url);
console.log(x.pathname.split('/').filter(x=>x).pop());
I have this string
/results?radius=4000&newFilter=true
and I need to replace radius=4000 with radius=n where n is a variable.
How can I use String.replace() method with regex to match that part?
You can use /radius=\d+/ to match "radius=" followed by any number of digits. With this we can use the replace() method to replace it with the desired value:
var str = "/results?radius=4000&newFilter=true";
var replacement = 123;
var newStr = str.replace(/radius=\d+/, "radius=" + replacement);
console.log(newStr);
If you want to get all parameters you can try this :
function getParams(uri) {
var params = {},
tokens,
re = /[?&]?([^=]+)=([^&]*)/g;
while (tokens = re.exec(uri)) {
params[decodeURIComponent(tokens[1])] = decodeURIComponent(tokens[2]);
}
return params;
}
var str='/results?radius=4000&newFilter=true';
str = str.substring(str.indexOf("?"));
params = getParams(str);
console.log(params);
console.log('radius => ', params['radius']);
This answer is from this post: How to get the value from the GET parameters?
It should be as easy as
var str='/results?radius=4000&newFilter=true';
var n = 1234;
str = str.replace(/(radius=)(\d+)/, "$1" + n);
var url = "/results?radius=4000&newFilter=true";
// or window.location.href for current url
var captured = /radius=([^&]+)/.exec(url)[1]; // your 4000
var newValue = 5000;
url = url.replace(captured, newValue);
by this way you can use it to get all your requested parameters too
and it is not decimal binded
ES6 with regex using positive lookbehind
const string = '/results?radius=4000&newFilter=true',
n = '1234',
changeRadius = (radius) => string.replace(/(?<=radius=)\d+/, n);
console.log(changeRadius(n));
/* Output console formatting */
.as-console-wrapper { top: 0; }
changeRadius is function that takes one parameter (radius) and performs replacement.
About the regex: \d+ gets as many digits as possible, (?<=STRING) is a positive lookbehind.
Other regex
Body of changeRadius() function can be replaced with string.replace(/radius=\d+/, 'radius=' + n). It probably has better performance, but original regex is more direct translation of the problem.
You can use capturing without remembering the match to capture only the numerical value after 'radius='.
var url = "/results?radius=4000&newFilter=true";
var radius = 123;
var newUrl = url.replace(/(?:radius=){1}(\d+)/, radius);
console.log(newUrl); // logs '/results?radius=4000&newFilter=true'0
'
I have a string var with following:
var str = getDataValue();
//str value is in this format = "aVal,bVal,cVal,dVal,eVal"
Note that the value is separated by , respectively, and the val is not fixed / hardcoded.
How do I replace only the bVal everytime?
EDIT
If you use string as the regex, escape the string to prevent malicious attacks:
RegExp.escape = function(string) {
return string.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&')
};
new RegExp(RegExp.escape(string));
var str = "aVal,bVal,cVal,dVal,eVal";
var rgx = 'bVal';
var x = 'replacement';
var res = str.replace(rgx, x);
console.log(res);
Try this
var targetValue = 'bVal';
var replaceValue = 'yourValue';
str = str.replace(targetValue , replaceValue);
I have a string like below
var indicator = -65(www.anyweb.com)
the number -65 can be any number too. How can I take out only the web url separately in javascript?
You need to extract the string after '(' and before ')'
var str = "-65(www.anyweb.com)";
str = str.substring(str.lastIndexOf("(")+1,str.lastIndexOf(")"));
You can use this example for string operations
var data = "-65(www.anyweb.com)";
var url = data.slice(data.indexOf('(')+1 ,data.indexOf(')')); console.log("URL :: ",url);
var domain = /\((.*?)\)/.exec("-65(www.anyweb.com)")[1];
console.log(domain);
The regex above will create a group with anything that's inside parenthesis.
You can use some simple string operations:
var str = "-65(www.anyweb.com)";
var url = "N/A";
// Find indices of open and close parentheses
var open = str.indexOf("(");
var close = str.lastIndexOf(")");
// If they were found then extract the URL from the string
if (open !== -1 && close !== -1) {
url = str.substring(open + 1, close);
}
console.log(url);
If you are more inclined to use regular expressions then this should do it:
var str = "-65(www.anyweb.com)";
var regex = /\((.*?)\)/; // Capture URL inside parentheses
var result = regex.exec(str); // Execute the regex against the string
var url = "N/A";
// If the URL was matched then assign it to the variable
if (result[1] !== undefined) {
url = result[1];
}
console.log(url);
You can also simply replace the stuff that you do not want:
var str = "-65(www.anyweb.com)";
str = str.replace(/^.*\(/, ""); // Remove everything before URL
str = str.replace(/\).*$/, ""); // Remove everything after URL
console.log(str);
Example 1 :
var data = "-65(www.anyweb.com)";
if(data.indexOf('(')!=-1){
var url = data.slice(data.indexOf('(')+1 ,data.indexOf(')'));
}
console.log("URL :: ",url);
Example 2 :
var data = "-65";
if(data.indexOf('(')!=-1){
var url = data.slice(data.indexOf('(')+1 ,data.indexOf(')'));
}
console.log("URL :: ",url);
Example 3 :
var data = "-65(www.anyweb.com)6764872";
if(data.indexOf('(')!=-1){
var url = data.slice(data.indexOf('(')+1 ,data.indexOf(')'));
}
console.log("URL :: ",url);
How do I get the last word in a URL that is URL between / and / ?
For example:
http://mywebsite.com/extractMe/test
http://mywebsite.com/extractMe
http://mywebsite.com/settings/extractMe/test
http://mywebsite.com/settings/extractMe
Here I would want to get extractMe from the URL.
If the URL is consistent, why not just use:
// Option 1
var url = "http://mywebsite.com/extractMe/test";
var extractedText = url.split("/")[3];
// Option 2
// If when a trailing slash is present you want to return "test", use this code
var url = "http://mywebsite.com/extractMe/test/";
var urlAry = url.split("/");
var extractedText = urlAry[urlAry.length - 2];
// Option 3
// If when a trailing slash is present you want to return "extractMe", use this:
var url = "http://mywebsite.com/extractMe/test/";
var urlAry = url.split("/");
var positionModifier = (url.charAt(url.length-1) == "/") ? 3 : 2;
var extractedText = urlAry[urlAry.length - positionModifier];
Here's a working fiddle: http://jsfiddle.net/JamesHill/Arj9B/
it works with / or without it in the end :)
var url = "http://mywebsite.com/extractMe/test/";
var m = url.match(/\/([^\/]+)[\/]?$/);
console.log(m[1]);
output:
test
This accounts BOTH for URLS like http://mywebsite.com/extractMe/test and http://mywebsite.com/extractMe/
function processUrl(url)
{
var tk = url.split('/');
var n = tk.length;
return tk[n-2];
}
Edited.
Regular Expression way:
var str = "http://example.com/extractMe/test";
var match = str.match(/\/([^\/]+)\/[^\/]+$/);
if (match) {
console.log(match[1]);
}