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 6946493
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:34:20+00:00 2026-05-27T13:34:20+00:00

If I have HTML like this: <dsometext<f<legit></legit> whatever What regex pattern do I use

  • 0

If I have HTML like this:
<dsometext<f<legit></legit> whatever

What regex pattern do I use to switch < to &lt; before d and f.

I think it’s all < which are not followed by a > but I can’t wrap the regex for that around my head. I have users typing HTML and then am using jQuery to wrap the HTML and parse the nodes, however bad interim markup blows it up, so I want to swap out the <

Ideas?

Edit

I’m not trying to parse the HTML to valid HTML. I just want to knock out interim characters as users type and the HTML is updated on page. If they are typing <strong>, and are still at the < and I try to put the HTML on the page, it will cause horrible markup. That’s why I need to swap it out.

Answer
I chose @pimvdb’s answer because it correctly answers the question I asked.

However to make the world happier, I found a much simpler way of doing things without using any regex. Basically I had an issue originally where [title] was in place of an element and it had no container element, guaranteed to just contain the title. Therefore changing innerHTML of anything would cause horrors. We simply added the wrapping element. The hesitation to do that and the cause of this thread was due to some crazy reasons specific to the app and backwards comparability for our users.

  • 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-27T13:34:21+00:00Added an answer on May 27, 2026 at 1:34 pm

    It’s not good practice to parse HTML with regexps, but this will do fine for your sample:

    "<dsometext<f<legit></legit> whatever".replace(/(?!<[^<>]+>)</g, "&lt;");
    

    The (?!<[^<>]+>) ensures that the < character to be replaced does not match the <...> pattern.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a html like this: <html> <head> <link rel=stylesheet type=text/css media=all href=style.css> </head>
Suppose I have an HTML snippet like this: <div> Hello <strong>There</strong> <div>I think <em>I
I have a html text like this: &lt;xml ... &gt; and I want to
I have html like this <h2 class=Comprehensive Leadership>Comprehensive Leadership</h2><a href=/topic/comprehensiveleadership>View all Comprehensive Leadership Programs
I have html code like this: <li> <a href=''>Example</a>&nbsp;<a href='' class='sub'>Edit</a>&nbsp;<a href='' class='sub'>Delete</a> </li>
I have HTML like this: <a href=# class=modalInput glow rel=#newsletter >Newsletter!</a> This opens a
So I have Html like this http://trac.edgewall.org/wiki/RecentChanges (I want to create some Flash Track
I have HTML code like this : <div> <a>Link A1</a> <a>Link A2</a> <a>Link A3</a>
I have html code like this <tr class=odd> <td><b>Reason for Termination:</b></td> <td>this has bunch
I have an html like this: <div id=container1> <div class=dragme>drag me</div> </div> <div id=container2>

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.