Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

87
Views
Javascript RegExp verify string finishes with html open tag + anything but a tag

I would like to verify that a string ends with any open Tag followed or not by a text

for example:

<div>some text here

or

<span>some text here

should match

but

<div>some text here</div>

or

<h1>some text here</h1>

should not

I tried to come up with a solution (sorry I'm not a regex pro)

let anyOpenTag = '<([^/>][^>])*>';
let anyCloseTag = '</[^/>][^>]*>';
let neitherOpenNorCloseTag = `[^(${anyOpenTag}|${anyCloseTag})]`;
let regex = new RegExp(
  escapeRegExp(`${anyOpenTag}${neitherOpenNorCloseTag}*$`),
  'gi');
  1. I set a variable "anyOpenTag" to a regex that verifies if it's an open tag like (<p>, <div>, <span> etc...

  2. I set a variable "anyCloseTag" to a regex that verifies if it's a closing tag like (</p>, </div>, etc...)

  3. I set a variable "neitherOpenNorCloseTag" that tries to combine the two and check if it's not one of them using the [^....]

finally I check if the regex match anyOpenTag + neitherOpenNorCloseTag

unfortunately it doesn't work for me, precisely the part that verifies "neitherOpenNorCloseTag"

your help is appreciated, even if you have a better regex I would be grateful

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Based on your examples, this snippet works:

const regex = /<.*>.*<\/.*>/g

const regexString = (regex) => {
  return (s) => {
    return !s.match(regex)
  }
}

const validateString = regexString(regex)

const strings = [
  '<div>some text here',
  '<span>some text here',
  '<div>some text here</div>',
  '<h1>some text here</h1>',
]

const validated = strings.map(validateString)

console.log('Result:', validated)

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!