I am trying to get URL params in a React Native app.
What I tried to do:
const parsedUrl = new URL(url)
// here the searchParams are empty list
console.log(parsedUrl.searchParams)
[Error: not implemented]
const parsedUrl = new URLSearchParams(url)
console.log(parsedUrl.get(param))
You Can Try this:
( Only For React Js )
function getURLParams(parameterName, url) {
let name = parameterName.replace(/[\[\]]/g, '\\$&');
let regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$)'), results = regex.exec(url);
if (!results) return null;
if (!results[2]) return null;
return decodeURIComponent(results[2].replace(/\+/g, ' '));
}
// For Current Window URL
console.log(getURLParams("param1", window.location.href));
// For Custom URL
console.log(getURLParams("param1", "https://example.com/index.html?param1=Hello"));
Or This
( For React Native )
var url = "http://example.com?param1=test¶m2=someData&number=123"
var regex = /[?&]([^=#]+)=([^&#]*)/g,
params = {},
match;
while (match = regex.exec(url)) {
params[match[1]] = match[2];
}
console.log(params)
const getSearchParamFromURL = (url, param) => {
const include = url.includes(param)
if (!include) return null
const params = url.split(/([&,?,=])/)
const index = params.indexOf(param)
const value = params[index + 2]
return value
}
const _url = 'myapp://?code=123123-1123123-418c-33213-123123¶m_key=param_value'
getSearchParamFromURL(_url, 'code') // output: 123123-1123123-418c-33213-123123
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With