0

I have an url like https://randomsitename-dd555959b114a0.mydomain.com and want to remove the -dd555959b114a0 part of the url.

So randomsitename is a random name and the domain is static domain name.

Is this possible to remove the part with jquery or javascript?

2
  • 1
    With JavaScript, yes (jQuery doesn't offer any improvements to regular expressions). Now, where did you get stuck? Commented Jun 22, 2014 at 15:20
  • This should work: yourUrlString.replace(/(https?:\/\/[a-zA-Z]+)-[^-]+(\.mydomain\.com\/?)/, '$1$2'). Commented Jun 22, 2014 at 15:22

3 Answers 3

1

Look at this code that is using regex

var url = "https://randomsitename-dd555959b114a0.mydomain.com";
var res = url.replace(/ *\-[^.]*\. */g, ".");

http://jsfiddle.net/VYw9Y/

Sign up to request clarification or add additional context in comments.

Comments

0

It's usually best to code for all possible cases and since hyphens are allowed within any part of domain names, you'll more than likely want to use a more specific RexExp such as:

^                  # start of string
(                  # start first capture group
    [a-z]+         # one or more letters
)                  # end first capture group
://                # literal separator
(                  # start second capture group
    [^.-]+         # one or more chars except dot or hyphen
)                  # end second capture group
(?:                # start optional non-capture group
    -              # literal hyphen
    [^.]+          # one or more chars except dot
)?                 # end optional non-capture group
(                  # start third capture group
    .+             # one or more chars
)                  # end third capture group
$                  # end of string

Or without comments:

^([a-z]+)://([^.-])(?:-[^.]+)?(.+)$

(Remember to escape slashes if you use the literal form for RegExps rather than creating them as objects, i.e. /literal\/form/ vs. new RegExp('object/form'))

Used in a string replacement, the second argument should then be: $1://$2$3

Previous answers will fail for URLs like http://foo.bar-baz.com or http://foo-bar.baz-blarg.com.

Comments

0

You could try this regex,

(.*)(-[^\.]*)(.*$)

Your code should be,

var url = "https://randomsitename-dd555959b114a0.mydomain.com";
var res = url.replace(/(.*)(-[^\.]*)(.*$)/, "$1$3");
//=>https://randomsitename.mydomain.com

Explanation:

  • (.*) matches any character 0 or more times and it was stored into group 1 because we enclose those characters within paranthesis. Whenever the regex engine finds -, it stops storing it into group1.

  • (-[^\.]*) From - upto a literal . are stored into group2. It stops storing when it finds a literal dot.

  • (.*$) From the literal dot upto the last character are stored into group3.

  • $1$3 at the replacement part prints only the stored group1 and 3.

OR

(.*)(?:-[^\.]*)(.*$)

If you use this regex, in the replacement part you need to put only $1 and $2.

DEMO

1 Comment

Since capturing is expensive, your second (ignored) capture group would be better as a non-capturing group, i.e. (?:...)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.