krv
krv

Reputation: 2920

JavaScript / Angular RegEx allow only alpahnumeric on paste

what i want to do is when the user pastes a big string i want to extract the alphanumeric value from the entered text.

I am doing this in plain JavaScript but angular answers are appreciated.

So far i have the following:

document.getElementById("paste").addEventListener("keyup",function(){
var inText= "Thank you for contacting us your code is abd123XYZ we will help you shortly ";

 reg = /^[a-z0-9]+$/i;
 var wordsArray=inText.split(" ");

 wordsArray.forEach(function(element){
   //   what to do here to only get abd123XYZ 
 });//foreach

});//keyup

Upvotes: 0

Views: 266

Answers (2)

gaurav bhavsar
gaurav bhavsar

Reputation: 2043

try this :

var reg = /^(?=.*[0-9])(?=.*[a-zA-Z])([a-zA-Z0-9]+)$/;
angular.forEach(inText.split(" "),function(d){
         if(reg.test(d))
         {
            console.log(d);
         }
});

Demo

Upvotes: 1

Avinash Raj
Avinash Raj

Reputation: 174706

Just check the each splitted parts with this regex.

reg = /^[a-z\d]*(?:[a-z]\d|\d[a-z])[a-z\d]*$/i;

And also note that ^[a-z0-9]+$ would also the words which has only alphabets or digits.

Upvotes: 0

Related Questions