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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:16:32+00:00 2026-06-13T23:16:32+00:00

I’m using this regular expression: var regex = /\<.*?.\>/g to match with this string:

  • 0

I’m using this regular expression:

var regex = /\<.*?.\>/g

to match with this string:

var str = 'This <is> a string to <use> to test the <regular> expression'

using a simple match:

str.match(regex)

and, as expected, I get:

["<is>", "<use>", "<regular>"]

(But without the backslashes, sorry for any potential confusion)

How can I get the reverse result? i.e. what regular expression do I need that does not return those items contained between < and >?

I tried /(^\<.*?\>)/g and various other similar combos including square brackets and stuff. I’ve got loads of cool results, just nothing that is quite what I want.

Where I’m going with this: Basically I want to search and replace occurences of substrings but I want to exclude some of the search space, probably using < and >. I don’t really want a destructive method as I don’t want to break apart strings, change them, and worry about reconstructing them.

Of course I could do this ‘manually’ by searching through the string but I figured regular expressions should be able to handle this rather well. Alas, my knowledge is not where it needs to be!!

  • 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-13T23:16:33+00:00Added an answer on June 13, 2026 at 11:16 pm

    Here’s a way to do custom replacement of everything outside of the tags, and to strip the tags from the tagged parts http://jsfiddle.net/tcATT/

    var string = 'This <is> a string to <use> to test the <regular> expression';
    // The regular expression matches everything, but each val is either a
    // tagged value (<is> <regular>), or the text you actually want to replace
    // you need to decide that in the replacer function
    console.log(str.replace( /[^<>]+|<.*?>/g, function(val){
        if(val.charAt(0) == '<' && val.charAt(val.length - 1) == '>') {
          // Just strip the < and > from the ends
          return val.slice(1,-1);
        } else {
          // Do whatever you want with val here, I'm upcasing for simplicity
          return val.toUpperCase(); 
        }
    } ));​
    // outputs: "THIS is A STRING TO use TO TEST THE regular EXPRESSION" 
    

    To generalize it, you could use

    function replaceOutsideTags(str, replacer) {
        return str.replace( /[^<>]+|<.*?>/g, function(val){
            if(val.charAt(0) == '<' && val.charAt(val.length - 1) == '>') {
              // Just strip the < and > from the ends
              return val.slice(1,-1);
            } else {
              // Let the caller decide how to replace the parts that need replacing
              return replacer(val); 
            }
        })
    }
    // And call it like
    console.log(
        replaceOutsideTags( str, function(val){
            return val.toUpperCase();
        })
    );
    
    • 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&#8217;Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm making a simple page using Google Maps API 3. My first. One marker
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am reading a book about Javascript and jQuery and using one of the

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.