반응형

 

Test()

문자열 사이에 영어가 있는지?(A ~ z)
  1. var username = 'JohnSmith';
  2. console.log(/[A-Za-z_-]+/.test(username)); //return true
  3. username = '한국인';
  4. console.log(/[A-Za-z_-]+/.test(username)); //return false
  5. username = '**A*';
  6. console.log(/[A-Za-z_-]+/.test(username)); //return true

Split()

문자를 띄워쓰기 기준으로 잘라 배열로 반환
  1. var str = 'this is my string';
  2. console.log(str.split(/\s/)); //logs : [this, is, my, string]
  3.  
  4. str = '이건 내가 작성한 문자열';
  5. var str2 = str.split(/\s/);
  6. console.log(str2[1]); //logs : 내가
  7. console.log(str2[3]); //logs : 문자열

Replace()

치환 정규식
  1. //ex1)
  2. var someString = 'Hello, World';
  3. someString = someString.replace(/World/, 'MyBlog');
  4. console.log(someString); //return Hello, MyBlog
  5. //ex2)
  6. var username = 'J!o!h!n!Smith!!~';
  7. username = username.replace(/[^A-Za-z\d_-]+/,'');
  8. //처음나오는 특수문자 제거
  9. console.log(username); // logs : Jo!h!n!Smith!!~
  10. //ex3)
  11. var username = 'J!o!h한n !Sm글ith!!~';
  12. username = username.replace(/[^A-Za-z\d_-]+/g,'');
  13. //문자열사이 특수문자(띄어쓰기포함) 전부제거
  14. console.log(username); // logs : JognSmith

Match()

매치되는 특정 문자 or 문자열 반환
  1. var name = 'UYEONG';
  2. console.log(name.match(/U/)); // logs "U"
  3. var str = "너는착한놈이야";
  4. console.log(str.match(/착한놈/)) // logs "착한놈"
  5.  
  6. var name = 'axaxaxsvsvsv';
  7. //검사하여 해당문자 배열로반환
  8. console.log(name.match(/a/g)) // logs ["a","a","a"]
  9.  
  10. var string = 'this is just a string with some 12345 and some !#$@# meixed in.!';
  11. console.log(string.match(/[a-z]+/gi));
  12. // logs : ["this", "is", "just", "a", "string", "with", "some", "and", "some", "meixed", "in"]
  13. console.log(string.match(/[a-z]+/gi)[1]); // logs : is

emailAdress :

이메일을 검사한후 문자열로 반환
  1. var email = 'uyeong21c@gmail.com';
  2. console.log(email.replace(/([a-z\d_-]+)@([a-z\d_-]+)\.[a-z]{2,4}/ig, '$1, $2'));
  3. // logs : uyeong21c, gamil

url :

url을 구조기준으로 잘라 반환
  1. function loc(url) {
  2. return {
  3. search : function() {
  4. return url.match(/\?(.+)/i)[1];
  5. },
  6. hash : function() {
  7. return url.match(/#(.+)/i)[1];
  8. },
  9. protocol : function() {
  10. return url.match(/(ht|f)tps?:/)[0];
  11. },
  12. href : function() {
  13. return url.match(/(.+\.[a-z]{2,4})/ig);
  14. }
  15. }
  16. }
  17. var l = loc('http://www.somesite.com?somekey=somevalue&anotherkey=anothervalue#theHashGoesHere');
  18. console.log(l);
  19. console.log(l.search());
  20. console.log(l.hash());
  21. console.log(l.protocol());
  22. console.log(l.href());





출처 - UYEONG's Blog(http://uyeong.tistory.com/189)

반응형

'공부거리 > HTML' 카테고리의 다른 글

input 박스 숫자만 입력받기  (0) 2011.12.23
[Script]팝업 띄우기  (0) 2011.12.08
스크립트로 추정되는 정규식(?)  (0) 2011.11.10
html 태그 정리  (0) 2011.11.07
기본적인 HTML 문  (0) 2011.10.26

+ Recent posts