简体   繁体   中英

Remove words inside the brackets

Hi I'm quite a newbie with javascript so I was wondering how do you remove the words inside the brackets & remove brackets? also I want to remove the words before the colon and then replace the brackets with a comma except for the last word. So for example if the string contains:

language:chinese (29)parody:hyouka (73)character:fuyumi irisu (12)houtarou oreki (12)group:shuudan bouryoku (41)artist:murasaki syu (49)misc:schoolgirl (11)nakadashi (11)acting like a pet (6)

then the output would become

chinese,hyouka,fuyumi irisu,houtarou oreki,shuudan bouryoku,murasaki syu,schoolgirl,nakadashi,acting like a pet

How can I do this with javascript? This is the code and it's not working

var str = "language:chinese (29)parody:hyouka (73)character:fuyumi irisu (12)houtarou oreki (12)group:shuudan bouryoku (41)artist:murasaki syu (49)misc:schoolgirl (11)nakadashi (11)acting like a pet (6)";

var tags = str.replace(/\s*\(.*?\)\s*/g, ','));
final = tags.replace(/.*(?=:)/i, "");
alert(final);

use [^)]+ to indicate that here is no closing bracket after opening:

var str = "language:chinese (29)parody:hyouka (73)character:fuyumi irisu (12)houtarou oreki (12)group:shuudan bouryoku (41)artist:murasaki syu (49)misc:schoolgirl (11)nakadashi (11)acting like a pet (6)";  

var tags = str.replace(/\s?\([^\)]+\)/g, ',')
              .replace(/[^:]+:([^,]+),/g,'$1,')
              .replace(/,$/,'');
alert(tags);​

even simple:

var tags = str.replace(/([^:]+:)?([^(]+)\s\([^\)]+\)/g,'$2,')
              .replace(/,$/,'');

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