Javascript/jquery bilan mag'lubiyatdan qanday matnni olib tashlash mumkin?

Ushbu urlni yoqtiraman:

http://abc-efff7b80a1e5b2.xyz.com

Men shunga o'xshash matnni olib tashlashni xohlayman:

http://abc.xyz.com

0
@Husen dinamik.
qo'shib qo'ydi muallif LazyCatIT, manba
@Husen dinamik.
qo'shib qo'ydi muallif LazyCatIT, manba
@Husen Bu tuzatish uzunligi yoki formati emas.
qo'shib qo'ydi muallif LazyCatIT, manba
@Husen Bu tuzatish uzunligi yoki formati emas.
qo'shib qo'ydi muallif LazyCatIT, manba
@Husen Bu tuzatish uzunligi yoki formati emas.
qo'shib qo'ydi muallif LazyCatIT, manba
Bu barqaror yoki dinamikmi?
qo'shib qo'ydi muallif Husen, manba
Biror narsa uzunligi yoki formati bo'lishi kerak. Iltimos, bunga oid ba'zi ma'lumotlarni bering.
qo'shib qo'ydi muallif Husen, manba
Biror narsa uzunligi yoki formati bo'lishi kerak. Iltimos, bunga oid ba'zi ma'lumotlarni bering.
qo'shib qo'ydi muallif Husen, manba
Ushbu statik matn "-efff7b80a1e5b2" yoki dinamik ravishda o'zgartirilsinmi?
qo'shib qo'ydi muallif Karan Singh, manba
@LazyCatIT agar matn - va. matnni hech narsa bilan almashtirmang ("")
qo'shib qo'ydi muallif Vas Hanea, manba
@LazyCatIT agar matn - va. matnni hech narsa bilan almashtirmang ("")
qo'shib qo'ydi muallif Vas Hanea, manba
@LazyCatIT agar matn - va. matnni hech narsa bilan almashtirmang ("")
qo'shib qo'ydi muallif Vas Hanea, manba

7 javoblar

Regex will be a fairly easy way to solve this.

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

<div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"> <div class="snippet-code">

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>
</div> </div>
3
qo'shib qo'ydi
@RanaGhosh Yaxshi so'zlar uchun cheksizdir
qo'shib qo'ydi muallif Carsten Løvbo Andersen, manba
Siz juda yaxshi regex yozishingiz mumkin. :)
qo'shib qo'ydi muallif Rana Ghosh, manba

Regex will be a fairly easy way to solve this.

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

<div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"> <div class="snippet-code">

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>
</div> </div>
3
qo'shib qo'ydi
@RanaGhosh Yaxshi so'zlar uchun cheksizdir
qo'shib qo'ydi muallif Carsten Løvbo Andersen, manba
Siz juda yaxshi regex yozishingiz mumkin. :)
qo'shib qo'ydi muallif Rana Ghosh, manba

Bu umuman ishlay olmasligiga amin emasman, ammo bu hiyla-nayrang bo'lishi mumkin.

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

<div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"> <div class="snippet-code">

  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>
</div> </div>
2
qo'shib qo'ydi

Shunda tuyalar:

<div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"> <div class="snippet-code">

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>
</div> </div>
1
qo'shib qo'ydi

Buning uchun tupurilgan dan foydalanishingiz mumkin. Iltimos, quyidagi kodni bajaring:

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);
1
qo'shib qo'ydi

Buning uchun tupurilgan dan foydalanishingiz mumkin. Iltimos, quyidagi kodni bajaring:

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);
1
qo'shib qo'ydi

Bu juda oson bo'lishi kerak. Qiymatni va almashtirish usulini qo'llang.

<div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"> <div class="snippet-code">

$('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"/>


<button type="button">Change value</button>
</div> </div>
1
qo'shib qo'ydi
Javascript UZB
Javascript UZB
99 ishtirokchilar

@js_uzb @vuejs_uz @react_uz @nodejs_uz @angular_uz @ngTashkent @yiiframework_uz @laravel_uz @linux_uzbek @python_uz @swift_uzb —————— @uzdevgroup @UzGeeksGroup ——— @UzDev_Jobs @jobs_uzb