The forum of the forums
Welcome to the Official Support Forum of Forumotion!

To take full advantage of everything offered by our forum, please log in if you are already a member, or join our community if you've not yet.



Create a free forum like this one.

Regex to get first word after slash in URL

View previous topic View next topic 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:
http://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.

Ange Tuteur
Forumaster

Male Posts : 13021
Reputation : 2684
Language : EN10, FR5
Location : Pennsylvania

http://fmdesign.forumotion.com

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

Ange Tuteur
Forumaster

Male Posts : 13021
Reputation : 2684
Language : EN10, FR5
Location : Pennsylvania

http://fmdesign.forumotion.com

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+).*/

Ange Tuteur
Forumaster

Male Posts : 13021
Reputation : 2684
Language : EN10, FR5
Location : Pennsylvania

http://fmdesign.forumotion.com

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

Ange Tuteur
Forumaster

Male Posts : 13021
Reputation : 2684
Language : EN10, FR5
Location : Pennsylvania

http://fmdesign.forumotion.com

Back to top Go down

View previous topic View next topic Back to top


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