简体   繁体   中英

match only the last instance of a pattern with Javascript regexp

I want to remove size data from a file name like

var src = 'http://az648995.vo.msecnd.net/win/2015/11/Halo-1024x551.jpg';
src = src.replace(
     /-\d+x\d+(.\S+)$/,
    function( match, contents, offset, s ) {
        return contents;
    }
);

this works as expected and i get

http://az648995.vo.msecnd.net/win/2015/11/Halo.jpg

But if I have a filename like

http://az648995.vo.msecnd.net/win/2015/11/slot-Drake-08-2000x1000-1024x512.jpg

it returns

http://az648995.vo.msecnd.net/win/2015/11/slot-Drake-08-1024x512.jpg

instead of the desired

http://az648995.vo.msecnd.net/win/2015/11/slot-Drake-08-2000x1000.jpg

Your regex does not work as expected primarily because of an unescaped dot in (.\\S+)$ part. An unescaped . matches any character but a newline. However, \\S matches any non-whitespace, including a . . Besides unnecessary backtracking, you may get an unexpected result with a string like http://az648995.vo.msecnd.net/win/2015/11/slot-Drake-08-2000x1000-1024x512.MORE_TEXT_HERE.jpg .

Assuming the extension is the part of a string after the last dot, you can use

-\d+x\d+(\.[^.\s]+)$

See regex demo

The nagated character class [^.\\s] matches any character but whitespace and a literal . symbol. Note that there is no point in using a callback function inside a replace, you can use a mere $1 backreference.

JS demo:

 var src = 'http://az648995.vo.msecnd.net/win/2015/11/slot-Drake-08-2000x1000-1024x512.jpg'; src = src.replace(/-\\d+x\\d+(.[^.\\s]+)$/, "$1"); document.body.innerHTML = src; 

Try escaping the . and you will be fine:

/-\d+x\d+(\.\S+)$/

略微改变正则表达式更明确一点:

/-\d+x\d+(\.[^\s-]+)$/

The regex can be simplified to the following

Replace

-\d+x\d+(\.\S+)

With

$1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM