Skip to content

Replace capture group while preserving surrounding capture #785

@tqwewe

Description

@tqwewe

Describe your feature request

I am unable to replace an nth's capture group when using Regex::replace/Regex::replace_all.

For example, I have the following regex: class(?:Name)?=["'](.*)["'] which matches class="container" and className='container'.

I need to replace only the container while preserving the surrounding text in the capture.

ex.replace_all(&content, |caps: &Captures| {
  let classes = caps.get(1).unwrap().as_str().to_owned() + " new-class";
  
  // What do I return here to keep the `class="` and `"` while replacing the content between?
})

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions