Regex - raqam va mag'lubiyatning boshqa satr bilan almashtirilishi

Men [biron-bir raqam] va [alohida satr] ning barcha [joy o'zgartirish]

For example if strings like 195 apples or 10 apples are found in a paragraph I need to replace this with 10 Oranges

Misol paragraf: Birinchi savatda 195 ta va ikkinchi savatda 10 ta olma mavjud.

Regexni almashtirgach, natijani olishim kerak Xulosa: Birinchi savatda 10 donadan iborat va ikkinchi savatda 10 ta apelsin mavjud.

Jqueryda men foydalanar edim

 myString.replaceAll("[(?i)string]", "anotherstring"); 

Buni PHP va Jquery-da qilish kerak.

Har kim menga yordam bera oladimi? Rahmat

1

6 javoblar

Haqida:

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

 
 var myString = 'There are 195 apples in the first basket and 10 apples in the second basket.';
myString = myString.replace("/(?i)\d+\s+apples\b/g", "10 Oranges");
console.log(myString);
</div> </div>
2
qo'shib qo'ydi
@ user4826347: mening tahrirlashga qarang.
qo'shib qo'ydi muallif Toto, manba
@ user4826347: Ha, shunday emasmi? (, savolingizni tahrirlab, foydalanadigan kodni qo'sha olasizmi?
qo'shib qo'ydi muallif Toto, manba
Tashakkur, lekin u ishlamadi!
qo'shib qo'ydi muallif user4826347, manba
hali ishlamadi! Ajoyib!
qo'shib qo'ydi muallif user4826347, manba

Haqida:

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

 
 var myString = 'There are 195 apples in the first basket and 10 apples in the second basket.';
myString = myString.replace("/(?i)\d+\s+apples\b/g", "10 Oranges");
console.log(myString);
</div> </div>
2
qo'shib qo'ydi
@ user4826347: mening tahrirlashga qarang.
qo'shib qo'ydi muallif Toto, manba
@ user4826347: Ha, shunday emasmi? (, savolingizni tahrirlab, foydalanadigan kodni qo'sha olasizmi?
qo'shib qo'ydi muallif Toto, manba
Tashakkur, lekin u ishlamadi!
qo'shib qo'ydi muallif user4826347, manba
hali ishlamadi! Ajoyib!
qo'shib qo'ydi muallif user4826347, manba

Search \d{1,}\s+(apples)

Replace 10 Oranges

1
qo'shib qo'ydi

Search \d{1,}\s+(apples)

Replace 10 Oranges

1
qo'shib qo'ydi

Quyidagi regex dan foydalaning:

(\d+\s(\w+))

** you can replace \w+ with your desired string

see demo/explanation

JavaScript

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

var str = "There are 195 apples in the first basket and 10 apples in the second basket.";
var result = str.replace(/(\d+\s(\w+))/ig, "10 Oranges");
console.log(result);
</div> </div>

PHP

$re = '/(\d+\s(\w+))/';
$str = 'There are 195 apples in the first basket and 10 apples in the second basket.';
$subst = '10 Oranges';
$result = preg_replace($re, $subst, $str);
echo $result;
1
qo'shib qo'ydi
@ user4826347 "\ w +" ni faqat apples bilan almashtiring
qo'shib qo'ydi muallif m87, manba
Qo'rqoq - u ishladi!
qo'shib qo'ydi muallif user4826347, manba
Avval ishlagan deb o'ylashdi. Hech narsa qilmadi ... Mana bu narsa, faqat olma emas, balki har bir so'zni ko'rib chiqish. agar 10 ta qulupnay bo'lsa, ular ham almashtiriladi
qo'shib qo'ydi muallif user4826347, manba

Bu ishlay boshladi

PHP

$re = '/(\d+\s((apples\b|apple\b)+))/';
$str = '195 apples in the first basket and 0 apple but 1 applex';
$subst = '10 Oranges';
$result = preg_replace($re, $subst, $str);
echo $result;

JQuery

var text1 = "10 apples is good";
var rrr = "10 Oranges";
var str = "There are 195 apple in the first basket and 10 apples in the second basket.1 apple";
var result = str.replace(/(\d+\s((apples\b|apple\b)+))/ig,"10 Oranges");
console.log(result);
0
qo'shib qo'ydi
PhP |BotsUz
PhP |BotsUz
93 ishtirokchilar

Phpni o'rganishni Hohlasangiz https://t.me/joinchat/AAAAAE-KRc5dd5tPMmGmWA A'zo bo'lin