vendredi 16 septembre 2016

JavaScript Regex: apostrophe TRUE for PC version, FALSE for mobile

I'm designing a quiz with user data input, which matches to the array of possible answers. The website is responsive and I want to give mobile users more slack with answers involving apostrophes, so they don't have to switch over their keybords to special symbols layout i.e.:

PC version: "that guy's snowboard" - ok; "that guys snowboard" - fail

Mobile version: "that guy's snowboard" - ok; "that guys snowboard" - ok

The code I've made so far:

var snowboardarr = ["that guy's snowboard"];
var snowboard = document.getElementById("snowboard");
var flag = false;
for (var i = 0; i < snowboardarr.length; i++)
{ if (snowboard.value.replace(/[^a-zA-Z0-9-=*@#№$%&^()"/]/g, '').toLowerCase() ==
snowboardarr[i].replace(/[^a-zA-Z0-9-=*@#№$%&^()"/]/g, '').toLowerCase()){flag = true;}
else if
(screen.width <= 650 && snowboard.value.replace(/[^a-zA-Z0-9-=*@#№$%&^()"/]/g, '').toLowerCase() ==
snowboardarr[i].replace(/[^a-zA-Z0-9-=*@#№$%&^()"/]/g, '').toLowerCase()){flag = true;} 
         }
if (flag) {snowboard.style.backgroundColor = "#83C183";}
else {snowboard.style.backgroundColor = "#E06969";}

I need, somehow, to exclude apostrophe ' from PC version's regEx and bind it to specific place, where it's meant to be:

that guy's snowboard - ok; that guys snowboard' - fail

Any help with this, guys, please?

Aucun commentaire:

Enregistrer un commentaire