This question already has answers here:
Split and parse window.location.hash
(3 answers)
Closed 2 years ago.
I have this url that is provided by the chrome.identity.getRedirectURL() function
https://jjjnkdmnhdhmfjdlmbljoiclmbojbiec.chromiumapp.org/#access_token=BQDhJnhA4NV2V-2Cn5xYwQyPz4QI5EdY3cu5nNqfgvVt4p4K8fKYtmlfp8ZQYS65ww2rUAZQ7chyZnPDZLlKJEyCfZBRxtr6Q1FpRe9UuiTJ2hT9SMNb-icodIc-I9ADauULDf4JVqvVXoHz1hWvpDWnqln8Yus&token_type=Bearer&expires_in=3600
I need to get the access_token param value to store the token and use it later with spotify api. What is the best method I can use in javascript?
If you have a url like https://some-url?access_token=1234 you can use URLSearchParams.
// window location search returns '?access_token=1234' if the url is 'https://some-url?access_token=1234'
const params = new URLSearchParams(window.location.search);
const token = params.get('access_token');
Update Regex
const x = new RegExp(/(#access_token=).*?(&)+/);
const str = 'https://jjjnkdmnhdhmfjdlmbljoiclmbojbiec.chromiumapp.org/#access_token=BQDhJnhA4NV2V-2Cn5xYwQyPz4QI5EdY3cu5nNqfgvVt4p4K8fKYtmlfp8ZQYS65ww2rUAZQ7chyZnPDZLlKJEyCfZBRxtr6Q1FpRe9UuiTJ2hT9SMNb-icodIc-I9ADauULDf4JVqvVXoHz1hWvpDWnqln8Yus&token_type=Bearer&expires_in=3600';
// Use String.prototype.match and pass the Regex
const result = String(str).match(x);
console.log(result);
// returns
[
"#access_token=BQDhJnhA4NV2V-2Cn5xYwQyPz4QI5EdY3cu5…9SMNb-icodIc-I9ADauULDf4JVqvVXoHz1hWvpDWnqln8Yus&",
"#access_token=", "&",
index: 57,
input: "https://jjjnkdmnhdhmfjdlmbljoiclmbojbiec.chromiuma…1hWvpDWnqln8Yus&token_type=Bearer&expires_in=3600",
groups: undefined
]
// Access the matching substring if there is one using
result[0];
// "#access_token=BQDhJnhA4NV2V-2Cn5xYwQyPz4QI5EdY3cu5nNqfgvVt4p4K8fKYtmlfp8ZQYS65ww2rUAZQ7chyZnPDZLlKJEyCfZBRxtr6Q1FpRe9UuiTJ2hT9SMNb-icodIc-I9ADauULDf4JVqvVXoHz1hWvpDWnqln8Yus&"
PLEASE NOTE THAT THE BEGINNING # AND ENDING & ARE INCLUDING WITH THIS REGULAR EXPRESSION.
Related
This question already has answers here:
How I can use "LIKE" operator on mongoose?
(7 answers)
Closed 1 year ago.
I am making a dictionary web app and now I am trying to make a search engine. I want to partly enter a word and get all of the similar matches. For example, I enter "ava", and get "lava" and "available" back. Mongoose has a method to do that but I need to enclose my query into slashes (like this /query/). I can do that with a string but I don't know how to put a variable in there in order to make a user search.
// executes, name LIKE john and only selecting the "name" and "friends" fields
await MyModel.find({ name: /john/i }, 'name friends').exec();
Here's my code:
app.post("/search", function(req, res){
let search = req.body.wordSearch;
console.log(req.body.wordSearch);
Dicword.find({word: /search/i}, function(err, searchRes) {
search = searchRes;
res.render("search", {search: search});
} );
})
I use let search to store the query and a result of a search but when I put it in find method with slashes /search/, it gets recognized as a string and not a variable. How can I use a variable there?
Instead of
{ word: /search/i }
Write like this
{ word: `/${search}/i` }
I have the following RRULE in a string format:
DTSTART;TZID=America/Toronto:20160909T040000RRULE:FREQ=WEEKLY;INTERVAL=1;BYDAY=MO,WE,FR;UNTIL=20161202T040000
I want to parse the properties of the string into their own respective variables to use inside form inputs to update. The same RRULE properties are going to show up in every string so I know for example that DTSTART will always be in the string.
I thought about using the string method search by specifying the property by its name and then adding the number of characters to add to get the position of the entity and want and then use .substring()
So for example, if I was trying to extract UNTIL, I could do:
export const parseUntilFromRRule = (rrule: string):Date => {
const posInRRule = rrule.search("UNTIL=");
const until = rrule.substring(posInRRule + 6);
return new Date(until);
};
However, for properties in the middle of the string, where the value's length may vary, this method would not work because I would not know the value of the second parameter to pass into substring
What generalized technique can I use to extract each RRULE property from the string?
I would use string split twice here:
var input = "DTSTART;TZID=America/Toronto:20160909T040000RRULE:FREQ=WEEKLY;INTERVAL=1;BYDAY=MO,WE,FR;UNTIL=20161202T040000";
var rrule = input.split("RRULE:")[1].split(";")[0];
console.log(rrule);
You can split by semicolons, then split each result by = if an entry contains a =, and turn the result into an object:
const input = `DTSTART;TZID=America/Toronto:20160909T040000RRULE:FREQ=WEEKLY;INTERVAL=1;BYDAY=MO,WE,FR;UNTIL=20161202T040000`;
const segments = input.split(';');
const entryKeyValues = Object.fromEntries(
segments.map(
segment => segment.includes('=')
? segment.split('=')
: [segment, '']
)
);
console.log(entryKeyValues);
console.log(entryKeyValues.UNTIL);
This question already has answers here:
How to get GET (query string) variables in Express.js on Node.js?
(26 answers)
Closed 5 years ago.
Let's say my url atm is
http://test.com/?city=toronto
I'm able to get the requestURL string that is
/?city=toronto
From here, I was wondering if there is a built in function or a standard procedure of extracting the word "toronto" or any other word that comes after the = from the string.
A standard procedure (as you mentioned) of doing this, you can get all the parameter values including value of city or any other parameter you may add to it.
var values = new URL('http://test.com/?city=toronto').searchParams.values();
for(var value of values){
console.log(value);
}
UPDATE
As #taystack mentioned in the comment, if you only want the value of a specific parameter (city) in this case, you can use:
new URL('http://test.com/?city=toronto').searchParams.get('city');
Use split();
var url = '/?city=toronto'.split('=');
console.log(url[1]);
Node.js has a new module called URL, which encodes the semantics of a url string. You don’t need to do any string manipulation.
const URL = require('url')
let my_url = new URL('http://test.com/?city=toronto')
URL#search returns the string representing the search:
my_url.search // '?city=toronto'
URL#query returns the search string excluding ?:
my_url.query // 'city=toronto'
and URL#searchParams returns an object encoding the search string:
my_url.searchParams // something kind of like {'city':'toronto'}
my_url.searchParams.get('city') // 'toronto'
my_url.searchParams.keys() // ['city'] (all the keys)
my_url.searchParams.values() // ['toronto'] (all the values)
I'm looking for a way to get the query string part of a url using node.js or one of it's extension module.
I've tried using url and express but they give me an array of parameters.
I want to original string.
Any ideas ?
example; for:
http://www.mydom.com?a=337&b=33
give me
a=337&b=33
(with or without the ?)
Use url.parse. By default it will return an object whose query property is the query string. (You can pass true as the second argument if you want query to be an object instead, but since you want a string the default is what you want.)
var url = require('url');
var urlToParse = 'http://www.mydom.com/?a=337&b=33';
var urlObj = url.parse(urlToParse);
console.log(urlObj.query);
// => a=337&b=33
How about using the built-in url.parse method?
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Get query string values in JavaScript
Parse query string in JavaScript
http://www.example.org/search?q=example&another=test&again=more
How can I create three sepearate variables in jQuery with the values example, test, and more?
In other words, how can I extract a query from a URL based on its position (first, second, third, etc.) in the URL or based on the &another= part (not sure what it's called) of the query?
I use this method:
var query = window.location.split('?')[1]; // or http://www.example.org/search?q=example&another=test&again=more
var query_obj = unserialize(query);
function unserialize(query) {
var pair, params = {};
query = query.replace(/^\?/, '').split(/&/);
for (pair in query) {
pair = query[pair].split('=');
params[decodeURIComponent(pair[0])] = decodeURIComponent(pair[1]);
}
return params;
}
So you'll have your variables in the query_obj like this: query_obj.test
I found the function over the internet a while ago so I can't provide a link. Sorry and thanks to whoever published it first.
I use an existing plugin for tasks like these: https://github.com/allmarkedup/jQuery-URL-Parser