簡體   English   中英

在 Python 的 re.sub() 的 Javascript `replace()` 中是否有等同於 $` 的東西?

[英]Is there an equivalent of $` in Javascript's `replace()` for Python's re.sub()?

在 JS中,你可以使用

  • $`插入匹配的 substring 之前的字符串部分。

  • $'插入匹配的 substring 之后的字符串部分。

獲得比賽前后的substring。

Python 的re.sub()中是否有與此等效的內容?

您可以將 function 傳遞給re.sub ,而不是替換字符串。 function 將收到一場比賽 object,並應返回比賽的替代品。

在 function 中,您可以使用match.start()match.end()獲取原始字符串中匹配項的開始和結束索引,並match.string獲取傳遞給re.sub的原始字符串。 因此,

match.string[:match.start()]

給出$`的效果,並且

match.string[match.end():]

給出$'的效果。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM