正则表达式

2016-08-02  本文已影响31人  菲龍探雲

Paste_Image.png

代码题

var text="   a b c   ";
function trim(str) {
    var rexp=/^\s+|\s+$/g;
    var newText;
    newText = str.replace(rexp,"");
  console.log(newText);
}
trim(text)
Paste_Image.png

http://js.jirengu.com/gejagudeni/1/edit


Paste_Image.png
http://js.jirengu.com/bumogozavo/1/edit Paste_Image.png
http://js.jirengu.com/bumogozavo/2/edit Paste_Image.png
http://js.jirengu.com/bumogozavo/3/edit Paste_Image.png
http://js.jirengu.com/bumogozavo/4/edit Paste_Image.png

http://js.jirengu.com/bumogozavo/5/edit

var re = /*正则...*/

var subj = "color: #121212; background-color: #AA00ef; width: 12px; bad-colors: f#fddee #fd2 "

alert( subj.match(re) )  // #121212,#AA00ef
Paste_Image.png

http://js.jirengu.com/bumogozavo/6/edit

var str = 'hello  "hunger" , hello "world"';
var pat =  /".*"/g;
str.match(pat);  

http://js.jirengu.com/bumogozavo/7/edit

Paste_Image.png
str = '.. <!-- My -- comment \n test --> ..  <!----> .. '
re = /.. your regexp ../

str.match(re) // '<!-- My -- comment \n test -->', '<!---->'

http://js.jirengu.com/novixefude/1/edit

Paste_Image.png
var re = /* your regexp */

var str = '<> <a href="/"> <input type="radio" checked> <b>'
str.match(re) // '<a href="/">', '<input type="radio" checked>', '<b>'

http://js.jirengu.com/novixefude/2/edit

Paste_Image.png

本教程版权归菲龍探雲和饥人谷所有,转载须说明来源

上一篇 下一篇

猜你喜欢

热点阅读