반응형
- 어떠한 형식에 맞춰 일치하는 결과를 가지고 올 수 있는 것

가끔 정규식을 분석해야 할 때가 있다. 그래서 한번 정리해 보았다.

 

정규식에 사용되는 기호

^    (caret) 문장의 시작

$    (dollar) 문장의 끝

.    (period) 줄바꿈 또는 단일문자 ex) h.t  => hat, hothit, hut, h7t. 등 검출

[]    (bracket) 문자의 집합이나 범위를 나타냄, 두 문자 사이에 – 는 범위를 나타냄

{}    (braced) {} 내의 숫자는 직전의 선행 문자 수를 나타냄

*    (asterisk) 직전의 선행문자가 0번 또는 그이상 나타냄

+    (asterisk) 직전의 선행문자가 1번 이상 나타냄

?    (asterisk) 직전의 선행문자가 0번 또는 1번 나타냄

|    (bar) or 을 나타냄

\    (backslash) 위에서 사용하는 특수문자를 정규식내에서 문자로 취급하고 싶을 때 \를 선행 기술

 

예1)

abc    abc 가 있는 것

^abc    abc 로 시작하는 것

abc$    abc 로 끝나는 것

^abc$    abc 로 시작하고 끝나는 것

[abc]    a,b,c 전부중 하나 포함한 경우

[a-z]    a 에서 z 중 하나 포함한 경우

^[0-9]    숫자 0~9 아무 숫자나 상관없으니 숫자로 시작하는 패턴을 찾는 것

[^0-9]    숫자가 들어있지 않는 패턴을 찾는 것

^[^0-9]    숫자가 들어있지 않는 문자로 시작하는 패턴을 찾는 것

a{3}    a 의 3번 반복인 aaa 인 것

a{3,}    a 가 3번 이상 반복인 것, aaa, aaaa, aaaaa

[0-9]{2}    두자리 숫자

abc[7-9]{2}    abc 포함 7에서 9까지 2자리 숫자 포함, abc77, abc78, abc97

 

예2) 대표적으로 많이 사용하는 정규식

 

이메일

"/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/"

 

집전화 : 지역번호가 들어간 전화번호 확인

"/^(070|02|031|032|033|041|042|043|051|052|053|054|055|061|062|063|064)-\d{3,4}-\d{4}$/u"

 

휴대폰번호 : 휴대폰 번호만 입력받을 경우

"/^(010|011|016|017|018|019)-\d{3,4}-\d{4}$/u"

 

우편번호

"/^\d{3}-?\d{3}$/u"

 

아이디 : 영숫자만 허용, 첫글자는 영문자로 시작, 4~8자리 이내로 입력

"/^[a-zA-Z]\w{2,7}$/u"

 

주민번호 : 숫자 13자리, 가운데 – 는 입력 해도 안해도 됨

"/^\d{2}[0-1]\d[0-3]\d-?[1-6]\d{6}$/u"

 

정규식을 이용한 치환

preg_replace( "치환될 정규식", "변환값","원본 문자열");

 

예) 태그 제거

 

iframe 제거 

$STRING=preg_replace("!<iframe(.*?)<\/iframe>!is","",$STRING);

 

&nbsp;  제거

$STRING=str_replace("&nbsp;"," ",$STRING);

 

복수 공백 하나로

$STRING=preg_replace("/\s{2,}/"," ",$STRING)

 

태그안에 style 속성 제거

$STRING=preg_replace("/ style=([^\"\']+) /"," ",$STRING); // style=border:0... 따옴표가 없을때
$STRING=preg_replace("/ style=(\"|\')?([^\"\']+)(\"|\')?/","",$STRING); // style="border:0..." 따옴표 있을때

 

태그안의 width=, height= 속성 제거

$STRING=preg_replace("/ width=(\"|\')?\d+(\"|\')?/","",$STRING);
$STRING=preg_replace("/ height=(\"|\')?\d+(\"|\')?/","",$STRING);

 

반응형
반응형



기존의 get() set() 은 없어졌고, socket object 에다가 직접 넣어서 쓰면 된다.


var http = require('http');

var socketio = require('socket.io');


var server = http.createServer();

var io = socketio.listen(server);

io.sockets.on('connection', function(socket){

//기존에 io.set('name') 이랫던거를... 

socket.on('setname', function(data){

socket.name = data;

});

});

반응형
반응형

 

 

반응형
반응형

 

 

반응형
반응형

 

 

/?parameter="test"

기존 request.param("parameter") 에서... 아래와 같이 업데이트됨;

 

var app = express();

app.post 일때, request.body.parameter

app.get 일때, request.params.parameter

 

어떤 방식으로 값이 들어오는지 보려면

console.log(request.params);

console.log(request.body);

console.log(request.query); 를 찍어보자..

 

또한

app.use(bodyParser.urlencoded({
    extended: true
}));

 

bodyParser 가 urlencoded 로 되어 있어야된다. JSON으로 되있을 경우 JSON 형식만 읽어오기떄문에 undefined가 줄기차게 뜰것임..ㅋㅋ

 

반응형
반응형


   『모던 웹을 위한 Node.js 프로그래밍』보면서 차근히 따라하고 있는데..  express에서 미들웨어 사용하다가 난관에 부딪혔다.


   logger 쓰려고 했더니, 에러가 빡!! 미들웨어가 더이상 번들로 설치되어 있는 게 아니니 따로 설치하란다. 이게 뭔...?


   또다시 구글링... 구글링구글구긆릉르르르ㅏ르르르르 


   http://stackoverflow.com/questions/23526356/heroku-node-js-server-crash-with-error-most-middleware-like-logger-is-no-lo


    ↑ 이 사람 글을 보니, 2가지 방법이 있댄다. logger 대신에 morgan을 설치해서 사용하든가, express가 설치된 폴더 안에서 package.json 파일을 찾아서 express 버전을 3.x대로 변경해주면 된다나.


   좀더 찾아보니 morgan이 4.x 버전에서는 logger을 대신해서 같은 역할을 하는 미들웨어란다.


   고로.. 4.x 버전에서는 이제 아래와 같이 써줘야 에러없이 잘 돌아간다. (npm install morgan으로 우선 morgan 설치해줘야 함)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// 모듈 추출
var express = require("express");
var morgan = require("morgan");
 
// 서버 생성
var app = express();
 
// 미들웨어 설정
// app.use(express.logger()); // 3.X 버전에서만 실행되고 4.X 버전에서는 에러 발생.
 
//app.use(morgan()); // 고로, 4.X 버전에서는 morgan을 사용해야 함. logger와 같은 역할.
app.use(morgan('dev'));
 
app.use(function (request, response, next) {
    response.send("<h1>come on express~~</h1>");
});
 
// 서버 실행
http.createServer(app).listen(1799, function () {
    console.log("Server Running at http://127.0.0.1:1799");
});



   http://blog.naver.com/PostView.nhn?blogId=rintiantta&logNo=40207854837


   ↑ 그리고 우연찮게 검색 중에 책의 저자가 운영하는 블로그를 발견했는데.. 헐... 뭐 바뀐 게 이리 많아?


   https://github.com/strongloop/express/wiki/Migrating-from-3.x-to-4.x


   ↑ 일단 이제 막 express 시작하는 참이라 아직 뭐가 뭔지 잘 모르겠지만,, 버전 업에 따른 변경 사항에 이것도 좀 참고해야겠다.

반응형
반응형
Installation
$ npm install cookie-parser

API

var express      = require('express')
var cookieParser = require('cookie-parser')

var app = express()
app.use(cookieParser())

cookieParser(secret, options)

  • secret a string used for signing cookies. This is optional and if not specified, will not parse signed cookies.
  • options an object that is passed to cookie.parse as the second option. See cookie for more information.
    • decode a function to decode the value of the cookie

cookieParser.JSONCookie(str)

Parse a cookie value as a JSON cookie. This will return the parsed JSON value if it was a JSON cookie, otherwise it will return the passed value.

cookieParser.JSONCookies(cookies)

Given an object, this will iterate over the keys and call JSONCookie on each value. This will return the same object passed in.

cookieParser.signedCookie(str, secret)

Parse a cookie value as a signed cookie. This will return the parsed unsigned value if it was a signed cookie and the signature was valid, otherwise it will return the passed value.

cookieParser.signedCookies(cookies, secret)

Given an object, this will iterate over the keys and check if any value is a signed cookie. If it is a signed cookie and the signature is valid, the key will be deleted from the object and added to the new object that is returned.

Example

var express      = require('express')
var cookieParser = require('cookie-parser')

var app = express()
app.use(cookieParser())

app.get('/', function(req, res) {
  console.log("Cookies: ", req.cookies)
})

app.listen(8080)

// curl command that sends an HTTP request with two cookies
// curl http://127.0.0.1:8080 --cookie "Cho=Kim;Greet=Hello"

 

반응형
반응형

 

 

http://stackoverflow.com/questions/4482686/check-synchronously-if-file-directory-exists-in-node-js

반응형

+ Recent posts