简体   繁体   中英

Regex to match beginning([) and and end(,) (only) in a string

This is my string:

[{ "name": "Neuromancer","genre": "cyberpunk" },

I want to remove the [ in the beginning and the come(,) in the end with the replace method.

I did this because I couldn't figure out how to write the Regex appropriately:

    line = line.replace(/\[/,"");
    line = line.replace(/\,$/,"");

Thank you :)

Combine both regexes using | . And also you need tp add ^ before \\[ , since you want to match [ char exists at the start. Inorder to make the regex engine to find two separate matches, you need to add global g modifier.

line = line.replace(/^\[|,$/g, "");

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