JavaScript取出字串中括號裡的內容

RaoMeng發表於2018-12-06

/**
 * 取出中括號內的內容
 * @param text
 * @returns {string}
 */
export function getBracketStr(text) {
    let result = ''
    if (isObjEmpty(text))
        return result
    let regex = /\[(.+?)\]/g;
    let options = text.match(regex)
    if (!isObjEmpty(options)) {
        let option = options[0]
        if (!isObjEmpty(option)) {
            result = option.substring(1, option.length - 1)
        }
    }
    return result
}

/**
 * 取出小括號內的內容
 * @param text
 * @returns {string}
 */
export function getParenthesesStr(text) {
    let result = ''
    if (isObjEmpty(text))
        return result
    let regex = /\((.+?)\)/g;
    let options = text.match(regex)
    if (!isObjEmpty(options)) {
        let option = options[0]
        if (!isObjEmpty(option)) {
            result = option.substring(1, option.length - 1)
        }
    }
    return result
}

複製程式碼

相關文章