You may use
See the regex demo. Details:
(@\[[^\]*])- Capturing group 1:
@[, 0 or more digits other than
]as many as possible and then
[^()]*?- 0 or more (but as few as possible) chars other than
:- a colon
\d+- 1+ digits
$1 in the replacement pattern refers to the value captured in Group 1.
const rx = /(@\[[^\]*])\([^()]*?:\d+\)/g; const remove_parens = (string, regex) => string.replace(regex, '$1'); let s = '@[admin](user:3) Testing this string @[hellotessginal](user:4) Hey!'; s = remove_parens(s, rx); console.log(s);
var str = "@[admin](user:3) Testing this string @[hellotessginal](user:4) Hey!"; str = str.replace(/ *\([^)]*\) */g, ' '); console.log(str);
You can replace matches of the following regular expression with empty strings.
str.replace(/(?<=\@\[(.*?)\])\(.*?:\d+\)/g, ' ');
I've assumed the strings for which
"user" are placeholders in the example cannot contain the characters in the string
"()". If that's not the case please leave a comment and I will adjust the regex.
I've kept the first capture group on the assumption that it is needed for some unstated purpose. If it's not needed, remove it:
There is of course no point creating a capture group for a substring that is to be replaced with an empty string.
(?<= : begin positive lookbehind \@\[ : match '@[' (.*?) : match 0+ chars, lazily, save to capture group 1 \] : match ']' ) : end positive lookbehind \(.*?:\d+\) : match '(', 0+ chars, lazily, 1+ digits, ')'
- How do I set profile image as first letters of first and last name?
- How to loop through string and check if there are unmatched letters
More Query from same tag
- Passing item through props
- React Rendering Dynamic Inner Components
- How to set cookie to next js application from separate server?
- How to use json-server | Getting 404 for local db.json
- Page wont refresh when using React Link and passing state
- Can I access React Env Variables in Another File?
- React Router where to use AJAX
- docker-compose stops immediately after starting react app created using create-react-app
- How can I fetch data with an array in react?
- How do I type a placeholder React element in another Component in TypeScript?
- Error while verifying token TypeError: Cannot read property 'data' of undefined firebase nodejs
- useEffect clears, then refills my react hook
- Changing text inside upload button after uploading
- how does promise and useState really work in React with AWS Amplify?
- How to use react to implement google Place's Searches, to create markers on the map?
- JSON data from firebase using object.entries
- ReactJS - toggle a class with an onclick
- How to change the state of a sibling component in ReactJS for a Master/Slave checkbox system?
- Change a state in react
- Adding and Removing Components React
- How do I reset the defaultValue for a React input
- How can I give a key in JSX the value of a variable depending on conditions
- How to properly set up material-ui with electron-react-boilerplate
- Passing Data from React Table to a Input TextField Material ui
- React click handler function is working after 2 clicks
- How to dynamically add class to parent div of focused input field?
- return undifined instead of rendering html tag
- Inserting function into file that extends component in React
- React types files not working in React TypeScript (.tsx) file - Throwing ESLint errors for React Component