Skip to content

Check if a string/text has balanced character or word pairs, e.g. '(', ')' or 'if', 'end'

License

Notifications You must be signed in to change notification settings

kaiwood/is-balanced

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

is-balanced

Installation

npm install is-balanced

Usage

This module checks for balanced things in a given string, mainly for parsing programming languages, HTML, etc.

Without additional arguments, it defaults to searching for parentheses.

const isBalanced = require("is-balanced");

let text = "((()))";
console.log(isBalanced(text)); // => true

text = "(()"
console.log(isBalanced(text)); // => false

For searching for e.g. curly braces:

let text = "{{}}";
console.log(isBalanced(text, "{", "}");

My personal need is for parsing Ruby code to find balanced if..end or def..end statements, so the opening and closing arguments work with multiple opening/closing arguments as well with regular expressions:

let text = `
if true
  [1, 2, 3].each do |number|
    puts number if number % 2 == 0
  end
end
`

console.log(isBalanced(text, [/^\s*?if/, "do", "def"], ["end"])) // => true

About

Check if a string/text has balanced character or word pairs, e.g. '(', ')' or 'if', 'end'

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published