gpt4 book ai didi

javascript - 之间的正则表达式匹配文本

转载 作者:行者123 更新时间:2023-11-29 16:23:56 25 4
gpt4 key购买 nike

我得到的字符串如下:

"some text here /word/ asdhd"
"some other likehere/word1/hahas"
"some other likehere/dhsad huasdhuas huadssad/h ah as/"

我需要的是获取两个斜杠 'word'、'word1'、'dhsad huasdhuas huadssad' 和 'h ah as' 之间的字符串。

什么是正则表达式?

最佳答案

编辑,以防您有多个这些词,并且想遍历它们。*由于问题已更改,请再次编辑。*

var myregexp = /\/(.+?)(?=\/)/g;
var match = myregexp.exec(subject);
while (match != null) {

// matched text: match[1]

match = myregexp.exec(subject);
}

解释:

    // \/(.+?)(?=\/)
//
// Match the character “/” literally «\/»
// Match the regular expression below and capture its match into backreference number 1 «(.+?)»
// Match any single character that is not a line break character «.+?»
// Between one and unlimited times, as few times as possible, expanding as needed (lazy) «+?»
// Assert that the regex below can be matched, starting at this position (positive lookahead) «(?=\/)»
// Match the character “/” literally «\/»

关于javascript - 之间的正则表达式匹配文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8096340/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com