Home > Software engineering >  JS. Check the string for such a view 120/80
JS. Check the string for such a view 120/80

Time:01-30

Please tell me, is it possible to check the string (in JavaScript) for such a view (exaple below). So that the first digit is two or three digits, then a slash (/) and then a two or three digit number.

Like this 100/10 or 20/30 or 200/500 or 40/500

Thanks in advance.

CodePudding user response:

Wrote a Regular Expression
First digit is any number between 1-9 (since you have omitted 0 from your examples). The next 1 or 2 digits can take any number between 0-9. separate with a / and repeat the first half.

const re = new RegExp('^[1-9][0-9]{1,2}[/][1-9][0-9]{1,2}$');

//true
console.log(re.test('100/10'))
console.log(re.test('20/30'))
console.log(re.test('200/500'))
console.log(re.test('40/500'))

//false
console.log(re.test('000/10'))
console.log(re.test('20/3'))
console.log(re.test('2/30'))
console.log(re.test('200/000'))
console.log(re.test('4000/500'))
console.log(re.test('4000/5000'))
.as-console-wrapper { max-height: 100% !important; top: 0; }

CodePudding user response:

Heres how to do it below. I explain with code comments


function isNumeric(value) {
    return /^\d $/.test(value);
}

const validString = (str)=>{
    const strArray = str.split("/"); // turn 100/10 -> ["100","10"]
    // If theres not two items in the strArray then it's false
    if (strArray.length!==2){
        return false;
    }
    
    const valueOne = strArray[0];
    const valueTwo = strArray[1];
    // Check that both strings are digits else false
    if (!isNumeric(valueOne) || !isNumeric(valueTwo)){
        return false;
    }

    // Return if there both two or three digits
    return (valueOne.length===2 || valueOne.length===3) && (valueTwo.length===2 || valueTwo.length===3)
}


console.log(validString("100/102")) // true
console.log(validString("20/30")) // true
console.log(validString("200/500")) // true
console.log(validString("40/500")) // true
console.log(validString("4000/500")) // false
console.log(validString("40a/500")) // false

CodePudding user response:

Try this one using Regular Expression:

let regexp = new RegExp('^[0-9]{2,3}/[0-9]{2,3}$');
//let regexp = new RegExp('^\\d{2,3}/\\d{2,3}$'); // this is also same as above
let value = '401/500';
console.log(value.search(regexp))
//0 means its matching the regex pattern
//-1 means pattern is not mathced in value

Refer this for regex in js

^ means beginning of string

& means end of string

[0-9] - it search for a digit between range from 0 to 9

{2,3} - it will check for atleast 2 occurances and atmost 3 occurances of [0-9]

CodePudding user response:

Yes, it is possible to have code in JS that does what you need. It can be achieved with regular expressions.

let string = "Like this 100/10 or 20/30 or 200/500 or 40/500";
let string2 = "40/500";

//exact match
let re = /^\d{2,3}\/\d{2,3}$/;

let isExactMatch = re.test(string); //false
let isExactMatch2 = re.test(string2); //true

//check that pattern occurs anywhere within the string
let re2 = /\b\d{2,3}\/\d{2,3}\b/;

let isMatch = re2.test(string); //true
let isMatch2 = re2.test(string2); //true

You can use regular expression to do much more, eg extract all matches from the string and also extract the individual values in each match and much much more.

  •  Tags:  
  • Related