简体   繁体   中英

JQUERY/JavaScript - RegEx everything after and including 3rd occurrence of character

I am trying to find some assistance in parsing out and replacing everything after (and including) the 3rd occurrence of a character with some text.

Here is an example of what I need accomplished:

Before: Bob,Jones,Suzy,Amy,Cindy,Jimmy

After: Bob,Jones,Suzy...

Ive gotten thus far:

var theString = ('Bob,Jones,Suzy,Amy,Cindy,Jimmy');
theString = theString.replace('', '...');

You can do split + slice + join :

var s = 'Bob,Jones,Suzy,Amy,Cindy,Jimmy'
var r = s.split(',').slice(0,3).join(',')
//=> "Bob,Jones,Suzy"

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