DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations

Trending

  • Integration Architecture Guiding Principles, A Reference
  • Docker Swarm vs. Kubernetes: Who Wins the Container War?
  • Creating Scalable OpenAI GPT Applications in Java
  • How to Create a Microservice Architecture With Java
  1. DZone
  2. Coding
  3. Languages
  4. Empty Character Class in JavaScript Regexes

Empty Character Class in JavaScript Regexes

In this blog, the reader will gain knowledge about Aba Search and Replace, which is a tool for replacing text in multiple files.

Peter Kankowski user avatar by
Peter Kankowski
·
May. 11, 23 · Code Snippet
Like (1)
Save
Tweet
Share
1.42K Views

Join the DZone community and get the full member experience.

Join For Free

I contributed to PCRE and wrote two smaller regular expression engines, but I still regularly learn something new about this topic. This time, it's about a regex that never matches.

When using character classes, you can specify the allowed characters in brackets, such as [a-z] or [aeiouy]. But what happens if the character class is empty?

Popular regex engines treat the empty brackets [] differently. In JavaScript, they never match. This is a valid JavaScript code, and it always prints false regardless of the value of str:

JavaScript
 
const str = 'a';
console.log(/[]/.test(str));


However, in Java, PHP (PCRE), Go, and Python, the same regex throws an exception:

Java
 
// Java
@Test
void testRegex1() {
    PatternSyntaxException e = assertThrows(PatternSyntaxException.class,
        () -> Pattern.compile("[]"));
    assertEquals("Unclosed character class", e.getDescription());
}


PHP
 
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);

// Emits a warning: preg_match(): Compilation failed: missing terminating ] for character class
echo preg_match('/[]/', ']') ? 'Match ' : 'No match';


Python
 
# Python
import re
re.compile('[]') # throws "unterminated character set"


In these languages, you can put the closing bracket right after the opening bracket to avoid escaping the former:

Java
 
// Java
@Test
void testRegex2() {
    Pattern p = Pattern.compile("[]]");
    Matcher m = p.matcher("]");
    assertTrue(m.matches());
}


PHP
 
<?php
echo preg_match('/[]]/', ']', $m) ? 'Match ' : 'No match'; // Outputs 'Match'
print_r($m);


Python
 
# Python
import re
print(re.match('[]]', ']')) # outputs the Match object


Go
 
// Go
package main

import (
    "fmt"
    "regexp"
)

func main() {
    matched, err := regexp.MatchString(`[]]`, "]")
    fmt.Println(matched, err)
}


This won't work in JavaScript because the first ] is interpreted as the end of the character class there, so the same regular expression in JavaScript means an empty character class that never matches, followed by a closing bracket. As a result, the regular expression never finds the closing bracket:

JavaScript
 
// JavaScript
console.log(/[]]/.test(']')); // outputs false


If you negate the empty character class with ^ in JavaScript, it will match any character, including newlines:

console.log(/[^]/.test('')); // outputs false
console.log(/[^]/.test('a')); // outputs true
console.log(/[^]/.test('\n')); // outputs true

Again, this is an invalid regex in other languages. PCRE can emulate the JavaScript behavior if you pass the PCRE2_ALLOW_EMPTY_CLASS option to pcre_compile. PHP never passes this flag.

If you want to match an opening or a closing bracket, this somewhat cryptic regular expression will help you in Java, PHP, Python, or Go: [][]. The first opening bracket starts the character class, which includes the literal closing bracket and the literal opening bracket, and finally, the last closing bracket ends the class.

In JavaScript, you need to escape the closing bracket like this: [\][]

console.log(/[\][]/.test('[')); // outputs true
console.log(/[\][]/.test(']')); // outputs true

In Aba Search and Replace, I chose to support the syntax used in Java/PHP/Python/Go. There are many other ways to construct a regular expression that always fails, in case you need it. So it makes sense to use this syntax for a literal closing bracket.

Aba Search and Replace

Replacing text in several files used to be a tedious and error-prone task. Aba Search and Replace solves the problem, allowing you to correct errors on your web pages, replace banners and copyright notices, change method names, and perform other text-processing tasks.

JavaScript PHP Go (programming language) Java (programming language) Python (language)

Published at DZone with permission of Peter Kankowski. See the original article here.

Opinions expressed by DZone contributors are their own.

Trending

  • Integration Architecture Guiding Principles, A Reference
  • Docker Swarm vs. Kubernetes: Who Wins the Container War?
  • Creating Scalable OpenAI GPT Applications in Java
  • How to Create a Microservice Architecture With Java

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: