w3schoon Posted February 9, 2013 Share Posted February 9, 2013 Nice to be with you everyone! I want to remove spaces using JavaScript and I found this in the web, var a=" W 3 S c h o o l s ";a.replace(/\s/g, ''); output: W3schools I learned that, replace(); - method searches a string for a specified value... http://www.w3schools.com/jsref/jsref_replace.asp \s - Find a whitespace character... http://www.w3schools.com/jsref/jsref_obj_regexp.asp /g - Perform a global match... http://www.w3schools.com/jsref/jsref_obj_regexp.asp '' - Empty string My question is, what is the purpose of "/" before \s? Thank you. Link to comment Share on other sites More sharing options...
callumacrae Posted February 9, 2013 Share Posted February 9, 2013 (edited) Regular expressions have the following syntax: /expression/flags The two forwards slashes (/) are what makes it a regular expression, just like strings have quotes around them. The expression is the regular expression itself, and the flags are stuff like telling the regular expression to be global ("g" is the modifier, not "/g"). I would recommend disregarding the w3schools regex ref you linked to, it isn't very good. Edited February 9, 2013 by callumacrae 1 Link to comment Share on other sites More sharing options...
w3schoon Posted February 11, 2013 Author Share Posted February 11, 2013 @callumacrae: Thanks a lot for the info. But why did you said, I would recommend disregarding the w3schools regex ref you linked to, it isn't very good. ? Can you give me very good link about regex? Link to comment Share on other sites More sharing options...
Don E Posted February 11, 2013 Share Posted February 11, 2013 This is a pretty good link: http://www.regular-expressions.info/ Link to comment Share on other sites More sharing options...
callumacrae Posted February 11, 2013 Share Posted February 11, 2013 This is a pretty good link: http://www.regular-expressions.info/ Yep, this is a very good tutorial by the author of "Regular Expressions Cookbook". If you want to know literally everything about regular expressions, buy Mastering Regular Expressions. It will make your head hurt, though. Link to comment Share on other sites More sharing options...
jeffman Posted February 11, 2013 Share Posted February 11, 2013 Mere mortals cannot know everything about regular expressions. That skill belongs to the gods. Link to comment Share on other sites More sharing options...
callumacrae Posted February 11, 2013 Share Posted February 11, 2013 I once worked with a guy who could write any regular expression without a reference. I learned from him to always understate your regex abilities, or you will spend half your time writing regular expressions. Link to comment Share on other sites More sharing options...
w3schoon Posted February 13, 2013 Author Share Posted February 13, 2013 Thanks a lot guys! I learned many things and I will start studying Regular Expressions! Link to comment Share on other sites More sharing options...
thescientist Posted February 13, 2013 Share Posted February 13, 2013 fwiw, the power of regex comes in finding patterns in strings. for a simple substitution method like in the OP, a regex might be considered excessive overhead. Link to comment Share on other sites More sharing options...
callumacrae Posted February 15, 2013 Share Posted February 15, 2013 fwiw, the power of regex comes in finding patterns in strings. for a simple substitution method like in the OP, a regex might be considered excessive overhead.The equivalent string operation would be pretty inefficient, though. Link to comment Share on other sites More sharing options...
Ingolme Posted February 15, 2013 Share Posted February 15, 2013 The equivalent string operation would be pretty inefficient, though.You mean this?a.split(" ").join(""); Benchmarking. 1,000,000 iterations: Split method: 1210msRegExp method: 1504ms Link to comment Share on other sites More sharing options...
callumacrae Posted February 15, 2013 Share Posted February 15, 2013 Oh, didnt read the original post properly (thought the OP wanted to remove duplicate spaces). Why don't you use .replace? Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now