JavaScript/regex: Remove text between parentheses
"Hello, this is Mike (example)".replace(/ *\([^)]*\) */g, "");
Result:
"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, '');
console.log(result);
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();}
console.log(clearWithNumbers(str1));
console.log(clearWithNumbers(str2));
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?
s/\([^)]*\)//
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:
console.log( "input[something][something2]".replace(/\[[^\][]*]$/, ''));
Related Topics
How to Save .Xlsx Data to File as a Blob
How to Output a Pdf Buffer to Browser Using Nodejs
How to Include External JavaScript Library in Reactjs
What Does This Format Means T00:00:00.000Z
How to Load All the Images from One of My Folder into My Web Page, Using Jquery/Javascript
Check Whether an Input String Contains a Number in JavaScript
Converting String Date in React/Javascript
How to Change Value of Object Which Is Inside an Array Using JavaScript or Jquery
Dynamically Add Variable Name Value Pairs to Json Object
Why Does Google Chrome Always Add Space After Selected Text
Save Embedded Pdf from Website
Click on Pseudo Element Using Selenium
Remove Whitespace Inside Json Object Key Value Pair
Get Directory of a File Name in JavaScript
Regex: Match a Word With Exactly One Occurrence of a Specified Character At the Beginning
Detect If User Is Using Webview for Android/Ios or a Regular Browser