简体   繁体   中英

Regex to Trim cobination of spaves and line breaks from a string

I've a string, and I want to trim all the leading and trailing combination of space and < br />. If the string is

"< br /> < br />< br />< br /> < br /> < br /> Hello < br /> < br /> < br /> < br /> < br /> < br /> < br /> < br /> < br /> < br />
"

I must get Hello. I unsuccessfully tried:

string = string.replace(/(<br \/>)+$| +$/g,'').replace(/^(<br \/>)+|^ +/g,'')

You could try this:

> var string = "< br /> < br />< br />< br /> < br /> < br /> Hello < br /> < br /> < br /> < br /> < br /> < br /> < br /> < br /> < br /> < br /> <br />";
> string.replace(/^(<\s*br\s*\/>|\s*)*|(<\s*br\s*\/>|\s)*$/gi, '');
"Hello"

You could try this. I think this should help you,

var a="< br /> < br />< br />< br /> < br /> <br /> Hello < br /> < br /> < br /> < br /> < br /> < br /> < br /> < br /> < br /> < br />";
var b=a.replace(/< br \/>|\s/g,"");
alert(b);//Hello

Cheers...!!!!!

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