Saturday, 18 May 2013

正規表現

imgFolder + 'PROJECTNAME/FILENAME.jpg'

を、

"./img/FILENAME.jpg"

に一括変換したい。

PROJECTNAMEとFILENAMEがそれぞれバラバラ。



(imgFolder \+ '.*g/)(.*)'

を

"./img/$2"

に置き換え。


--------------------------------------------
大基本
/←この間に書く→/


基本テクニック


| : OR検索
/http|https|ftp/
httpまたはhttpsまたはftpという意味


var text = "hello world. http://www.weburl.com/";
text = text.replace(/http|https|ftp/, "???");


/*--
hello world. ???://www.weburl.com/
--*/

----------------------------------------------


[] : この中の文字にマッチするもの。
[A-Za-z0-9]
で大文字、小文字、数字にマッチ。


[A-Za-z0-9.:\/?&#=+-_~!%@()]
でよく使う記号も大体マッチ。


var text = "hello world. http://www.weburl.com/";
text = text.replace(/[A-Za-z0-9.:\/?&#=+-_~!%@()]/, "???");


/*--
???ello world. http://www.weburl.com/
--*/


[]+ : で単語単位になる


var text = "hello world. http://www.weburl.com/";
text = text.replace(/[A-Za-z0-9.:\/?&#=+-_~!%@()]+/, "???");


/*--
??? world. http://www.weburl.com/
--*/

/←この間に書く→/g
とすると、何回でも適用。(逆に言うと、最後に g をつけないと最初にマッチしたものだけ適用。)


var text = "hello world. http://www.weburl.com/";
text = text.replace(/[A-Za-z0-9.:\/?&#=+-_~!%@()]+/g, "???");


/*--
??? ??? ???
--*/


----------------------------------------------

() 文字列をグループ化できる。
var text = "hello world. http://www.weburl.com/";
text = text.replace(/(http|https|ftp)([A-Za-z0-9.:\/?&#=+-_~!%@()]+)/g, "???");
/*--
hello world.  ???
--*/

No comments:

Post a Comment