Regex to get first word after slash in URL

Go down

Solved Regex to get first word after slash in URL

Post by Ahmed.K on July 6th 2014, 5:43 am

for example, this link:
https://help.forumotion.com/f43-scripts-coding-problems

I want get the forum id (f43).

I tested with this one but doesn't work.
var url = window.location.pathname.replace(/^\/([^\/]*).*$/, '$1');

Any ideas?

Ahmed.K
Forumember

Posts : 349
Reputation : 4
Language : English

Back to top Go down

Solved Re: Regex to get first word after slash in URL

Post by Ange Tuteur on July 7th 2014, 6:11 am

Hello Ahmed.K,

You can use \d to find digits. ex :
Code:
/f\d+/.test(location.pathname)

You can also use parentheticals to capture the value for use in replacements.
avatar
Ange Tuteur
Forumaster

Male Posts : 13201
Reputation : 2917
Language : EN10, FR5, JA5
Location : Macungie, PA

https://github.com/SethClydesdale

Back to top Go down

Solved Re: Regex to get first word after slash in URL

Post by Ahmed.K on July 7th 2014, 10:45 pm

Doesn't work.
Well, here is the idea...
I try to add a dynamic forum-id in this html code:
<input type="text" name="forum" value="FORUM_ID" />
Any ideas?

Ahmed.K
Forumember

Posts : 349
Reputation : 4
Language : English

Back to top Go down

Solved Re: Regex to get first word after slash in URL

Post by Ange Tuteur on July 8th 2014, 6:58 pm

Did you capture the id from the pathname ?
Code:
var n = location.pathname.replace(/\/f(\d+).*/,'$1'); // the result of n is the forum ID
avatar
Ange Tuteur
Forumaster

Male Posts : 13201
Reputation : 2917
Language : EN10, FR5, JA5
Location : Macungie, PA

https://github.com/SethClydesdale

Back to top Go down

Solved Re: Regex to get first word after slash in URL

Post by Ahmed.K on July 8th 2014, 8:13 pm

No. i want adding the "f" too with it.

And...

<input type="text" name="forum" value="" id="forumid" />
<script type="text/javascript">
$(function(){
var url = location.pathname.replace(/\/f(\d+).*/,'f$1');
$("#forumid").prop("value", url);
});
</script>

and it works fine, but, is there something better than this?

Ahmed.K
Forumember

Posts : 349
Reputation : 4
Language : English

Back to top Go down

Solved Re: Regex to get first word after slash in URL

Post by Ange Tuteur on July 8th 2014, 8:14 pm

If you want the f with it, capture the f too. Wink

Code:
/\/(f\d+).*/
avatar
Ange Tuteur
Forumaster

Male Posts : 13201
Reputation : 2917
Language : EN10, FR5, JA5
Location : Macungie, PA

https://github.com/SethClydesdale

Back to top Go down

Solved Re: Regex to get first word after slash in URL

Post by Ahmed.K on July 8th 2014, 8:30 pm

Ah, ok, it works fine. Thanks Ange.

Ahmed.K
Forumember

Posts : 349
Reputation : 4
Language : English

Back to top Go down

Solved Re: Regex to get first word after slash in URL

Post by Ange Tuteur on July 8th 2014, 8:56 pm

You're welcome

Topic archived

Have a good day. Smile
avatar
Ange Tuteur
Forumaster

Male Posts : 13201
Reputation : 2917
Language : EN10, FR5, JA5
Location : Macungie, PA

https://github.com/SethClydesdale

Back to top Go down

Back to top


 
Permissions in this forum:
You cannot reply to topics in this forum