score:3
You may use
s.replace(/(@\[[^\][]*])\([^()]*?:\d+\)/g, '$1')
See the regex demo. Details:
(@\[[^\][]*])
- Capturing group 1:@[
, 0 or more digits other than[
and]
as many as possible and then]
\(
- a(
char[^()]*?
- 0 or more (but as few as possible) chars other than(
and)
:
- a colon\d+
- 1+ digits\)
- a)
char.
The $1
in the replacement pattern refers to the value captured in Group 1.
See the JavaScript demo:
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);
score:0
Try this:
var str = "@[admin](user:3) Testing this string @[hellotessginal](user:4) Hey!";
str = str.replace(/ *\([^)]*\) */g, ' ');
console.log(str);
score:0
You can replace matches of the following regular expression with empty strings.
str.replace(/(?<=\@\[(.*?)\])\(.*?:\d+\)/g, ' ');
I've assumed the strings for which "admin"
and "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:
(?<=\@\[.*?\])\(.*?:\d+\)
There is of course no point creating a capture group for a substring that is to be replaced with an empty string.
Javascript's regex engine performs the following operations.
(?<= : begin positive lookbehind
\@\[ : match '@['
(.*?) : match 0+ chars, lazily, save to capture group 1
\] : match ']'
) : end positive lookbehind
\(.*?:\d+\) : match '(', 0+ chars, lazily, 1+ digits, ')'
Source: stackoverflow.com
Related Query
- Javascript how to identify a combination of letters and strip a portion of it
- How do I set profile image as first letters of first and last name?
- how to add react jsx as javascript file type in vim and enable eslinting, auto completions?
- How can I refactor by moving files in a JavaScript project and have references update automatically?
- How to merge and unzip (.zip, .z01, .z02) in javascript (client side only)?
- How to upload image using javascript fetch api and express multer
- How to export and import class properly in javascript ES6
- How to add HTML elements in to JavaScript object and use in react
- JavaScript how to wait until a function and all its child functions are done
- How to write CSS / SCSS / PostCSS inside React .tsx file as JavaScript String in VSCode and WebStorm?
- How to send an email using javascript and not change the window location?
- How does Tiktok.com and Youtube shorts automatically play video on scroll (on mobile)? React Javascript
- How to reduce and get value from object array using javascript
- How do you embed and call an external javascript function in a react component?
- How to place the popup based on the position of another div element using javascript and react?
- How to mock the JavaScript window.open and window.close?
- How leetcode do this: Read user's input String as multiple JavaScript functions and run a main function with user's input params
- How to remove "[" and "]" signs from array in Javascript
- How to take a comma separated array and convert it into individual list items? React.js & Javascript
- How do you remove a nested property with spread and rest in javascript
- How to read an image from the localhost url and get its width and height in javascript
- How to display the date month and year in a specific format using javascript and react?
- How to retrieve string after @ character and before space using javascript or react?
- How to use useMemo instead of a local state using javascript and react?
- How to make a function as pure function using javascript and react?
- How to loop through string and check if there are unmatched letters
- How to compare each elements in an Array and group the ones that have same data (in my case date) ? JavaScript
- How to get the month and year from the past date untill now JavaScript
- How to animate a conditionally rendered part of the component on state change using javascript and css?
- How to use purgeCSS in create-react-app to remove unused css and javascript
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
- Reassign value of variable with javascript destructuring
- 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