Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- let url = 'https://www.emag.bg/tavanen-sushilnik-prostor-familia-7-pryta-po-2-0-m-24144780/pd/D82CCJBBM/?ref=fp_growth_atc_3&provider=rec&recid=rec_74_5882c9dbb4c8568aa518710f54be8a643981adb4e51b0506246d48b33c640d10_1673361687&scenario_ID=74';
- let parseUrl = (str) =>{
- let result = {}
- let protocol = '';
- let host = '';
- let path ='';
- result['query'] = {};
- let a = str.indexOf('://')
- protocol = str.substring(0,a)
- let b = str.indexOf('/', a+3)
- if( b === -1){
- b = str.length;
- }
- host = str.substring(a+3,b)
- let c = str.indexOf('?',b+1)
- if(c === -1){
- c = str.length;
- }
- path = str.substring(b,c)
- result['protocol'] = protocol;
- result['host'] = host;
- result['path'] = path;
- let id = true
- let lable = false
- let tempKey = [];
- let tempValue =[]
- for (let i = c+1 ; i<= str.length - 1; i++){
- if(str[i] !== '&' && str[i] !== '='){
- if(id){
- tempKey.push(str[i])
- } else if (lable) {
- tempValue.push(str[i])
- }
- } else if (str[i] !== '&' || str[i] !== '='){
- if(id)
- {tempKey.push(' ')}
- if(lable){
- tempValue.push(' ')
- }
- id = !id;
- lable = !lable
- }
- }
- tempKey = tempKey.join('').split(' ')
- tempKey.pop()
- tempValue = tempValue.join('').split(' ')
- for (let i = 0 ; i <= tempKey.length-1;i++){
- result['query'][tempKey[i]] = tempValue[i]
- }
- return result;
- }
- console.log(parseUrl(url));
Advertisement
Add Comment
Please, Sign In to add comment