Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6748639
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:35:22+00:00 2026-05-26T12:35:22+00:00

The below matches when there’s excess white space. Now I need it to match

  • 0

The below matches when there’s excess white space. Now I need it to match regardless of what’s in between it… I tried replacing \s* with .+? but that didn’t work. I’m looking at a regex cheat sheet and can’t figure out the most efficient way to do this.

preg_replace("/<sup>(\s*)$key(\s*)<\/sup>/i", "<sup>$val</sup>", $text);

Should match: <sup>(anything here){$key}(anything here)</sup>

update

This is sad… I just realized the regex I’m working with doesn’t need to match whitespaces, but a <br> tag… something like

$text = preg_replace("/<sup>([\s\n(<br>)]*)$key([\s\n(<br>)]*)<\/sup>/is", "<sup>$val</sup>", $text);

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T12:35:22+00:00Added an answer on May 26, 2026 at 12:35 pm

    I’m not entirely sure I understand, but if you want .*? to also match newlines, you need the /s modifier:

    preg_replace("#<sup>(.*?)$key(.*?)</sup>#is", "<sup>$val</sup>", $text);
    

    Be aware though, that this might match more than you think. If you have more than one <sup> tag, and only the last one contains $key, the regex will match all the way from the first <sup> to the last </sup>! So better be specific about what you want to allow there.

    I’d suggest you use (?:(?!</?sup).)* instead of .*? in the above regex. This ensures that you’ll never match across tag boundaries:

    (?:       # Try to match...
     (?!      #  (as long as it's impossible to match
      </?sup #   <sup or </sup
     )        #   at the current position)
     .        # ... any character.
    )*        # Do this any number of times.
    

    So, in the end, you get:

    preg_replace("#<sup>(?:(?!</?sup).)*$key(?:(?!</?sup).)*</sup>#is", "<sup>$val</sup>", $text);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Below code not work, but it's work fine for jsf1.2. Now the framework is
I know there is plenty of question answered over here https://stackoverflow.com/questions/tagged/youtube+regex , but not
Is there a function similar to jQuery .closest() but for traversing descendants and returning
I came across an implementation of the nearest neighbor algorithm for finding matches between
Is there a way to ensure that the box around a plot matches the
I want to create array of javascript's regexp match string from the text below.
I need help with a regular expression that will find matches in the strings
I need to create a script to search through just below a million files
Given a string text which contains newline there is a search keyword which matches
I am using the below code for example: SELECT *, (MATCH (`wm`, `locn`, `gns`)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.