Javascript/Regex: Remove Text Between Parentheses

JavaScript/regex: Remove text between parentheses

"Hello, this is Mike (example)".replace(/ *\([^)]*\) */g, "");


"Hello, this is Mike"

Javascript/regex: Remove text between square brackets

If you prefer doing this using a regular expression, consider the following.

var r = 'Hello, this is Mike [example]'.replace(/ *\[[^\]]*]/, '');
console.log(r); //=> "Hello, this is Mike"

If your data contains more text in brackets you want removed, use the g (global) modifier.

Based off your given string, you could just use split here.

var r = 'Hello, this is Mike [example]'.split(' [')[0]
console.log(r); //=> "Hello, this is Mike"

JavaScript/regex: Remove text between brackets in string

Here is how you can do it:

const input = '<p>Hello, how are you doing <strong>today</strong></p>';
const result = input.replace(/<[^>]+>/g, '');

Remove text inside parentheses at the end of the string

You may use

.replace(/\s*\([^()]*\)$/, '')

See the regex demo.

If you need to make sure you only remove horizonatal whitespaces*, replace the \s* with [^\S\r\n]*.

Pattern explanation:

  • \s* - zero or more whitespaces (replace with [^\S\r\n] to match horizontal whitespace, i.e. \s with \r and \n "subtracted" from it)
  • \( - a literal ( symbol
  • [^()]* - zero or more symbols other than ( and ) (replace with [^)]* if there can be ( inside the parentheses)
  • \) - a closing )
  • $ - end of string.

var re = /\s*\([^()]*\)$/g; var strs = ['09373 837 937 (mobile)','9838373838 (home)','+19383947388 (home)','(938)3947388 (mobile)'];for (var s = 0; s < strs.length; s++) {                  // Demo  document.body.innerHTML += "Replacing in \"<i>" + strs[s] + "</i>\"... ";  document.body.innerHTML += "Result: <b>\"" + strs[s].replace(re, '') + "\"</b><br/>";}

JS Regex: Remove Text in Parentheses if starts with digit

The start ^ and end $ symbols refer to the start and end of the input string. Remove them.

var str1 = "String 1 (12:30am - 5:00pm)";var str2 = "String 2 (Parentheses) (3:00am - 3:10am)";
function clearWithNumbers(str) { return str.replace(/\(\d.*\)/g, '').trim();}

Remove text between two square brackets in javascript

There is expression without regarding nested [[]] myString.replace(/\[\[[^\]]*\]\]/g,''); It is tricky thing to track nested including of pair of symbols

How can I remove text within parentheses with a regex?


So in Python, you'd do:

re.sub(r'\([^)]*\)', '', filename)

Remove text between square brackets at the end of string

Note that \[.*?\]$ won't work as it will match the first [ (because a regex engine processes the string from left to right), and then will match all the rest of the string up to the ] at its end. So, it will match [something][something2] in input[something][something2].

You may specify the end of string anchor and use [^\][]* (matching zero or more chars other than [ and ]) instead of .*?:


See the JS demo: