RainerS
RainerS

Reputation: 521

Do a simple seach regardless of upper an lower case

Could someone explain to me, how I can do in javascript this simple code, without taking care of upper and lower case?

if(res.search('em')!=-1){  unit='em'; res.replace(unit,'');}
if(res.search('vh')!=-1){  unit='vh'; res.replace(unit,'');}
if(res.search('px')!=-1){  unit='px'; res.replace(unit,'');}

Without any idea, that is what I have coded. It's a lot of code

if(res.search('Em')!=-1){  unit='Em'; res.replace(unit,'');}
if(res.search('eM')!=-1){  unit='eM'; res.replace(unit,'');}
if(res.search('EM')!=-1){  unit='EM'; res.replace(unit,'');}
...

I'm sure there is a better way to do that!?

Thanks a lot.

Upvotes: 0

Views: 72

Answers (5)

Cat
Cat

Reputation: 4226

All you need to to is force your string to lowercase (or uppercase) before testing its contents:

if( res.toLowerCase().search('em') !== -1){ do(stuff); }

To handle replacing the actual substring value in res, something like this should work:

let caseInsensitiveUnit = "em";
let unitLength;
let actualUnit;
let position = res.toLowerCase().search(caseInsensitiveUnit);
if(position > -1){ 
    unitLength = caseInsensitiveUnit.length; 
    actualUnit = res.substring(postion, position + unitLength);
    res.replace(actualUnit, "");
}

Upvotes: 0

customcommander
customcommander

Reputation: 18901

If you can make these three assumptions:

  1. The string always starts with a number
  2. The string always ends with a unit
  3. The unit is always two characters

Then it could be as simple as:

const str = '11.5px';
const unit = str.substr(-2); //=> 'px'
const value = parseFloat(str, 10); //=> 11.5

Or with a function:

const parse = str => ({unit: str.substr(-2), value: parseFloat(str, 10)});
const {unit, value} = parse('11.5px');
// unit='px', value=11.5

Upvotes: 0

Mark
Mark

Reputation: 92440

You could use a regular expression with replace and save the found unit as a side effect of the replacer function. This would allow you to replace the unit without searching the string twice:

let res = "Value of 200Em etc."
let unit
let newRes = res.replace(/(em|vh|px)/i, (found) => {unit = found.toLowerCase(); return ''})

console.log("replaced:", newRes, "Found Unit:", unit)

Upvotes: 1

Code Maniac
Code Maniac

Reputation: 37755

For the first part you can use toLowerCase()

if(res.toLowerCase().search('em') != -1)

You can use alternation in regex alongside case insensitive flag.

/(em|vh|px)/i Mathces em or vh or px.

function replaceUnit(input){
  return input.replace(/(em|px|vh)/i ,'replaced')
}

console.log(replaceUnit('height: 20em'))
console.log(replaceUnit('width:=20Em'))
console.log(replaceUnit('border-radius: 2Px'))
console.log(replaceUnit('unit=pX'))
console.log(replaceUnit('max-height=20Vh'))

Upvotes: 0

Taki
Taki

Reputation: 17654

you can use toLowerCase(), transform all the string to lower case and compare,

var tobereplaced = 'em';

if(res.search.toLowerCase(tobereplaced)> -1){   res.replace(tobereplaced,'');}

Upvotes: 0

Related Questions