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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T05:29:12+00:00 2026-06-10T05:29:12+00:00

I’m using RegExp to extract specific words from a string like the following: <div class = ''></div>class class 

  • 0

I’m using RegExp to extract specific words from a string like the following:

<div class = ''></div>class class 

My current RegExp statement is this:

/(<)[^(>)]*(;| |'|&quote;)(class)?( |'|&quote;|=)/gi

In this instance I wan t to match the word ‘class’ but only if it has specific characters (or ampersands) before and after it. This statement matches:

<div class 

out of the original string, I’m using this in a replace method (to insert text where ‘class’ was) and I want to keep the other character around it, is there any way of trimming this match down to only select(replace) the word ‘class’ (in this instance)?

Or is there a better way of doing this?
Thanks in advance.

  • 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-06-10T05:29:13+00:00Added an answer on June 10, 2026 at 5:29 am

    You can use next code to do this job:

    var removeSubstring=(function(){
        var fn=function($0,$1,$2,$3){return $1+$3;};
        return function(str,before,after,removed){
            var rg=new RegExp("("+before+")("+removed+")("+after+")","gi");
            return str.replace(rg,fn);
        };
    })();
    
    var str="<div class = ''></div>class class <div class ";
    var before="<div ";
    var after=" ";
    var removed="class";
    
    removeSubstring(str,before,after,removed);
    // <div  = ''></div>class class <div  
    

    But you must ensure what in before, after and removed strings does not contained special RegExp format characters (such as . [ ] ? * and others, see MDN RegExp). Otherwise you must first escape these special characters in before, after and removed strings.

    UPDATE:

    You also can use valid regular expression for before, after and removed to make more flexible search. For example next code remove letters ‘X’, ‘V’ and ‘Z’ but not remove ‘x’, ‘v’ and ‘z’:

    var str="qXd ezrwsvrVa etxceZd";
    var before="q|w|e|r";
    var after="a|s|d|f";
    var removed="z|x|c|v";
    
    removeSubstring(str,before,after,removed);
    // "qd ezrwsvra etxced"
    

    If you need to replace substring with another string you can use next function:

    function replaceSubstring(str,before,after,removed,inserted){
        var rg=new RegExp("("+before+")("+removed+")("+after+")","gi");
        return str.replace(rg,function($0,$1,$2,$3){
            // $1 is searched "before" substring
            // $2 is searched "removed" substring
            // $3 is searched "after" substring
            return $1+(inserted||"")+$3;
        });
    };
    
    
    var str="qXd ezrwsvrVa etxceZd";
    var before="q|w|e|r";
    var after="a|s|d|f";
    var removed="z|x|c|v";
    replaceSubstring(str,before,after,removed,"DDDD")
    // "qDDDDd ezrwsvrDDDDa etxceDDDDd"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string
I've got a string that has curly quotes in it. I'd like to replace
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
I'm new to using the Perl treebuilder module for HTML parsing and can't figure

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.