# Grap first part of url using javascript

**URL:** <https://community.glideapps.com/t/grap-first-part-of-url-using-javascript/64858>\
**Category:** Ask for Help\
**Created:** [August 9, 2023, 3:15pm UTC](https://community.glideapps.com/t/grap-first-part-of-url-using-javascript/64858 "2023-08-09T15:15:52Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Krivo](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/krivo/32/5363_2.png) [@Krivo](https://community.glideapps.com/u/Krivo)\
**Post date:** [August 9, 2023, 3:15pm UTC](https://community.glideapps.com/t/grap-first-part-of-url-using-javascript/64858/1 "2023-08-09T15:15:52Z")

</div>

Wonder if somebody can help me get some javascript running

I have a url like:

> [https://res.cloudinary.com/XYZ/image/upload/IMG\_5158.jpg](https://res.cloudinary.com/XYZ/image/upload/IMG_5158.jpg)

I need to put in some parameters like:

> [https://res.cloudinary.com/XYZ/image/upload/MYPARAMETERS/IMG\_5158.jpg](https://res.cloudinary.com/XYZ/image/upload/MYPARAMETERS/IMG_5158.jpg)

The filename can be grapped by the javascript as seen in the post below.

But I need to get the first part:

> [https://res.cloudinary.com/XYZ/image/upload/](https://res.cloudinary.com/XYZ/image/upload/)

How can that be done? Or is there a better solution to obtain what I trying to do 🙂

_NB: Seems that all the javascript columns that I create will give the same result even though I change the javascript code. Anyone else seeing the same behavior?_

Referred post:

> [@Get part of url](https://community.glideapps.com/t/get-part-of-url/63275/4):
>
> I believe it gives you escape characters in some cases. var segments = p1.split('/'); // Get the last segment var lastSegment = segments[segments.length - 1]; // Split the last segment by question mark or hash, if present var fileName = lastSegment.split(/[?#]/)[0]; // Decode URL-encoded characters var decodedFileName = decodeURIComponent(fileName); // Return the decoded file name return decodedFileName; Added a step to decode those.

---

<div class="post-metadata">

**Author:** ![Darren\_Murphy](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/darren_murphy/32/47326_2.png) [@Darren\_Murphy](https://community.glideapps.com/u/Darren_Murphy)\
**Post date:** [August 9, 2023, 3:36pm UTC](https://community.glideapps.com/t/grap-first-part-of-url-using-javascript/64858/2 "2023-08-09T15:36:40Z")

</div>

Try this:

```auto
const bits = p1.split('/');
const last_bit = bits.pop();
return bits.join('/') + `/${p2}/${last_bit}`;

```

 ![CleanShot 2023-08-09 at 23.35.42@2x](https://us1.discourse-cdn.com/flex002/uploads/glideapps/original/3X/8/c/8cbd0cce0179c405e4634577aea1f1dba08ebed8.jpeg)

---

<div class="post-metadata">

**Author:** ![Darren\_Murphy](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/darren_murphy/32/47326_2.png) [@Darren\_Murphy](https://community.glideapps.com/u/Darren_Murphy)\
**Post date:** [August 9, 2023, 3:47pm UTC](https://community.glideapps.com/t/grap-first-part-of-url-using-javascript/64858/3 "2023-08-09T15:47:03Z")

</div>

@Krivo if you want a way to do this without JavaScript, you could use two Extract Matching Text columns to pull the URL and Filename into two separate columns, and then create a new URL (inserting your parameters) with a template.

Something like this:

 ![CleanShot 2023-08-09 at 23.45.14@2x](https://us1.discourse-cdn.com/flex002/uploads/glideapps/original/3X/5/7/57f574bb6bcbdc98655616dbddc467abe4b85372.png)

Regular Expression for the first column: `^(.*)\/`  
Regular Expression for the second column: `^.*\/(.*)$`

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/system/32/53398_2.png) [@system](https://community.glideapps.com/u/system)\
**Post date:** [August 16, 2023, 3:47pm UTC](https://community.glideapps.com/t/grap-first-part-of-url-using-javascript/64858/4 "2023-08-16T15:47:04Z")

</div>

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.
