Faqat ikkita so'z, bitta bo'sh joy va 50 belgigacha cheklov berish uchun Regex

Quyidagi shartlar bilan muntazam ifodani qurishga harakat qilaman:

  • Faqat ikki so'z
  • so'nggi so'zdan keyin bitta bo'sh joy qoldiring
  • Maksimal uzunlik 50

("familiyasi familiyasi")

Rahmat.

1
Uzunligi bajarish uchun (? =. {, 50}) kabi oldinga qarata foydalaning.
qo'shib qo'ydi muallif user663031, manba
Uzunligi bajarish uchun (? =. {, 50}) kabi oldinga qarata foydalaning.
qo'shib qo'ydi muallif user663031, manba

10 javoblar

Buni ko'ring:

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

const validate = data =>  /^\w+\s*\w+ ?$/.test(data) && data.length <= 50

const testData = ['this works', 'this    too ', '  this fails', 'firstname lastname ', ' firstname middlename lastname ']

for (let temp of testData) {
    console.log(`${temp} ${validate(temp)}`)
}
</div> </div>
1
qo'shib qo'ydi

Buni ko'ring:

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

const validate = data =>  /^\w+\s*\w+ ?$/.test(data) && data.length <= 50

const testData = ['this works', 'this    too ', '  this fails', 'firstname lastname ', ' firstname middlename lastname ']

for (let temp of testData) {
    console.log(`${temp} ${validate(temp)}`)
}
</div> </div>
1
qo'shib qo'ydi

Mana, sizning barcha talablaringizni qamrab oladigan regex, jumladan, uzunlik tekshiruvi:

^(?!.{51,})(\w+\s+\w+ ?)$

Izoh:

^(?!.{51,})    assert that the string is not followed by 51 or more characters
(
    \w+        match a word
    \s+        followed by one or more spaces
    \w+        followed by a second word
     ?         ending with an optional space
)$

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

function tests(str){
  var regex = /^(?!.{51,})(\w+\s+\w+ ?)$/;
  if (regex.test(str)) {
      console.log("passes");
  }
  else {
      console.log("fails");
  }
}
tests("firstname lastname "); //passes
tests("first name last name");//fails
tests("123456789 1234567890123456789012345678901234567890");//passes (length 50)
tests("123456789 12345678901234567890123456789012345678901");//fails (length 51)
</div> </div>

Regex 101

1
qo'shib qo'ydi

Mana, sizning barcha talablaringizni qamrab oladigan regex, jumladan, uzunlik tekshiruvi:

^(?!.{51,})(\w+\s+\w+ ?)$

Izoh:

^(?!.{51,})    assert that the string is not followed by 51 or more characters
(
    \w+        match a word
    \s+        followed by one or more spaces
    \w+        followed by a second word
     ?         ending with an optional space
)$

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

function tests(str){
  var regex = /^(?!.{51,})(\w+\s+\w+ ?)$/;
  if (regex.test(str)) {
      console.log("passes");
  }
  else {
      console.log("fails");
  }
}
tests("firstname lastname "); //passes
tests("first name last name");//fails
tests("123456789 1234567890123456789012345678901234567890");//passes (length 50)
tests("123456789 12345678901234567890123456789012345678901");//fails (length 51)
</div> </div>

Regex 101

1
qo'shib qo'ydi

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

function tests(str){
  var pat=/^[a-zA-Z]+\s[a-zA-Z]+\s?$/;
  if(pat.test(str) && str.length<=50){
    console.log("true");
  }
  else{
    console.log("false");
  }
}
tests("firstname lastname "); //true
tests("first name last name"); //flase
</div> </div>
0
qo'shib qo'ydi

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

function tests(str){
  var pat=/^[a-zA-Z]+\s[a-zA-Z]+\s?$/;
  if(pat.test(str) && str.length<=50){
    console.log("true");
  }
  else{
    console.log("false");
  }
}
tests("firstname lastname "); //true
tests("first name last name"); //flase
</div> </div>
0
qo'shib qo'ydi

Siz shu qatorda qatorni bo'linib, so'zlar sonini tekshirishingiz mumkin

var array = "some string".split(" "); //result - ["some","string"]

Keling, so'zlar sonini tekshirishingiz mumkin

var count = array.length; //will return 2

Bu kabi harflarni hisoblashingiz mumkin

var letterCount = "some string".length; //will return 11

Regexni ishlatishdan osonroq va yaxshiroq yo'l.

0
qo'shib qo'ydi

Siz shu qatorda qatorni bo'linib, so'zlar sonini tekshirishingiz mumkin

var array = "some string".split(" "); //result - ["some","string"]

Keling, so'zlar sonini tekshirishingiz mumkin

var count = array.length; //will return 2

Bu kabi harflarni hisoblashingiz mumkin

var letterCount = "some string".length; //will return 11

Regexni ishlatishdan osonroq va yaxshiroq yo'l.

0
qo'shib qo'ydi

This pattern will help /^[a-Z]+\s[a-Z]+\s?$/

<input type="text" pattern="^[a-Z]+\s[a-Z]+\s?$" title="Must be two words...">
0
qo'shib qo'ydi

This pattern will help /^[a-Z]+\s[a-Z]+\s?$/

<input type="text" pattern="^[a-Z]+\s[a-Z]+\s?$" title="Must be two words...">
0
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