0

I have a url likes this:

http://abc-efff7b80a1e5b2.xyz.com

I would like to remove some text like that:

http://abc.xyz.com

6
  • 2
    Is this fixed or dynamic ? Commented Mar 17, 2017 at 6:59
  • 1
    Is this static text "-efff7b80a1e5b2" or changed dynamically ? Commented Mar 17, 2017 at 6:59
  • @Husen It is dynamic. Commented Mar 17, 2017 at 7:01
  • Something should be fixed length or format. Please provide some information regarding this. Commented Mar 17, 2017 at 7:04
  • @Husen It isn't fix length or format. Commented Mar 17, 2017 at 7:05

5 Answers 5

3

Regex will be a fairly easy way to solve this.

.replace(/\-.*?\./, '.') this replaces all text between - and .

var str = "http://abc-efff7b80a1e5b2.xyz.com";
str = str.replace(/\-.*?\./, '.');
console.log(str)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

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

1 Comment

@RanaGhosh Cheers for the kind words
2

Not sure if it will work generally, but this might do trick.

>>a
"http://abc-efff7b80a1e5b2.xyz.com"
>>re
>>a.replace(a.match(re)[0], "")
"http://abc.xyz.com"    

  function sanitize(url, regx) {
    return url.replace(url.match(regx)[0], "");
  }
Array.from(document.getElementsByTagName('tr')).forEach(e=> e.children[1].innerHTML = sanitize(e.children[0].innerHTML, /(?:\-\w+)/));
<table>
<tr><td>http://abc-8n0qhnb9.xyz.com</td><td></td>></tr>
<tr><td>http://abc-q265r4by.xyz.com</td><td></td>></tr>
<tr><td>http://abc-xiwugpkf.xyz.com</td><td></td>></tr>
<tr><td>http://abc-wbojtv02.xyz.com</td><td></td>></tr>
<tr><td>http://abc-wf5aw904.xyz.com</td><td></td>></tr>
<tr><td>http://abc-vp37wiex.xyz.com</td><td></td>></tr>
<tr><td>http://abc-y04xag44.xyz.com</td><td></td>></tr>
<tr><td>http://abc-gw5nz2v0.xyz.com</td><td></td>></tr>
<tr><td>http://abc-0lu1r5bb.xyz.com</td><td></td>></tr>
<tr><td>http://abc-2r1o6r0l.xyz.com</td><td></td>></tr>
<tr><td>http://abc-bhs9gapw.xyz.com</td><td></td>></tr>
<tr><td>http://abc-j1b0ickq.xyz.com</td><td></td>></tr>
<tr><td>http://abc-8be4kis9.xyz.com</td><td></td>></tr>
<tr><td>http://abc-hcufvqot.xyz.com</td><td></td>></tr>
<tr><td>http://abc-eeeze0w9.xyz.com</td><td></td>></tr>
<tr><td>http://abc-0ya1j44x.xyz.com</td><td></td>></tr>
<tr><td>http://abc-jmquwmc2.xyz.com</td><td></td>></tr>
<tr><td>http://abc-6rjg674m.xyz.com</td><td></td>></tr>
<tr><td>http://abc-tajrtltl.xyz.com</td><td></td>></tr>
<tr><td>http://abc-y2k01kmn.xyz.com</td><td></td>></tr>
<tr><td>http://abc-rujlhgjf.xyz.com</td><td></td>></tr>
<tr><td>http://abc-r18o2cm9.xyz.com</td><td></td>></tr>
<tr><td>http://abc-23lemwng.xyz.com</td><td></td>></tr>
<tr><td>http://abc-bvsa71rj.xyz.com</td><td></td>></tr>
<tr><td>http://abc-ydxycm8w.xyz.com</td><td></td>></tr>
<tr><td>http://abc-79rwm0td.xyz.com</td><td></td>></tr>
<tr><td>http://abc-cpe62kkn.xyz.com</td><td></td>></tr>
<tr><td>http://abc-6u5uby9k.xyz.com</td><td></td>></tr>
<tr><td>http://abc-2a032pvi.xyz.com</td><td></td>></tr>
<tr><td>http://abc-1c0poy7q.xyz.com</td><td></td>></tr>
</table>

Comments

1

You can use spilt for doing this. Please follow the below code::

var url = 'http://abc-efff7b80a1e5b2.xyz.com';
var split_url = url.split('.');
var start_url = split_url[0].split('-')[0];
var full_url = start_url + '.' + split_url[1] + '.' + split_url[2];
console.log(full_url);

Comments

1

It should be very easy. Get the value and the use the replace method.

$('button').click(function(e){
  e.preventDefault();
  var str = $('#txt').val();
  var regex = /(https?:\/\/abc)(\-.+)(.xyz.com)/ig
  str= str.replace(regex, "$1$3")
  $('#txt').val(str)
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" value="http://abc-efff7b80a1e5b2.xyz.com" id="txt"/>
<br/>
<br/>
<button type="button">Change value</button>

Comments

1

Ty this:

var url = "http://abc-efff7b80a1e5b2.xyz.com";
url = url.replace(/-.*x/, '.x');
console.log(url);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Comments

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.