Regex

Regular Expression


@TonyQ


                                                                                                  #JavaScript.tw 

What's regex


String manipulation tool.

Begin with basic problems 


I got a message from remote server:
var text = "Here's your price [$10]";

And I need to get the $10 back.




find Features


var text = "Here's your price [$10]";


var start = text.indexOf("[");
var end text.indexOf("]");

console.log(text.substring(start +1 ,end));

In other thought


var text = "Here's your price [$10]";


var start = text.indexOf("$");
var words = [];
for(var i = start+1 ; i < text.length ;++i){
   if(!isNaN(text[i] )){
      words.push(text[i]);
   }
} console.log(words.join(""));

console.log(text.substring(start +1 ,end));

Regex is hard


match tonylovejava@gmail.com 

(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])


deck

By TonyQ Wang

deck

  • 2,011
Loading comments...

More from TonyQ Wang