Reputation: 2462
To start with it has to be compatible with all modern browsers (since it is used on mobile devices). What do I want to do?
Well I have one meta tag that I have to check, because people write it differently into their DOM.
So my example is below. I need to remove spacing and decimals if they are existing.
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
So I get the content of that meta:
var getViewport = document.querySelector('meta[name=viewport]');
var check = getViewport.getAttribute('content');
console.log(check); // output: width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no
I am not even able to trim it. I tried some trimming functions such as:
el.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
But it does not do anything in this particular case.
I need to compare if that content matches, else I will replace it with proper content. People write it on many ways, so I don't want to compare each possible way, but instead make it all look the same, so if exists then it would be same match as my example above.
width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no
width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no
width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no
width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=no
And they can also add some spacing and remove others on some places by purpose or accidentaly... If you understand what I am trying to say. This would probably need some editing due to fast typing and grammar issues.
Bottom line, can anyone teach me how to trim, remove decimals from the "var check"
so any case if it exists would match this type of string: width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no
Upvotes: 0
Views: 180
Reputation: 15810
There's another, more comprehensive approach you could do, if you want to allow for "any order" of the arguments.
Remove ".0", split on the commas, trim the spaces, sort the list, and re-join.
For example:
check = check.replace(/\.0*\b/g, "").split(/\s*,\s*/g).sort().join(",");
This would allow the following to match:
width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no
user-scalable=no,maximum-scale=1.0,initial-scale=1.0,width=device-width
See it in action? JS Bin
Upvotes: 3
Reputation: 1259
First you normalize it:
var your_string = 'width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no';
var your_comparison_string = your_string.replace(/\s/g,'');
After that you could search for any floating numbers. In this case it is sufficient to search for those WITH dot because you will norm to those without:
var search = your_comparison_string.match(/\d\.\d/g);
for (var i in search) {
your_comparison_string = your_comparison_string.replace(search[i], parseInt(search[i]));
}
Upvotes: 1
Reputation: 15810
You'd need a regular expression to remove all spaces, and all ".0". This will do it:
check = check.replace(/(\s+|\.0*\b)/g, "")
Upvotes: 2