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
- How to set up nested routes in react-redux application?
- React Typescript and Jest
- How do I pass the "isVisibility" value as a prop to another component (React)
- TypeError: bcLi.push(...) is not a function
- How to make babel-plugin-transform-imports work
- dropdown is not showing state value on page load
- "Child already has a parent, it must be removed first" when i'm reordering a table in react
- pass prop from map method into parent component
- #dom-to-image Export an off the screen div as image
- Can't type in some fields of the nested state
- React not setting hook state despite using hook setter
- fix missing dependency warning when missing an object in useEffect React Hook?
- Reactjs map method pick unique id
- React - Prevent focus going out of modal when tabbing
- React Enzyme - Test `componentDidMount` Async Call
- Object.assign does not update my state (React)
- how check if doc was updated using updateOne() [mongoose,React , Node js, Mongodb]
- How to concate string outside loop using map() method in jsx?
- Prevent `useSelector` from re-rendering component?
- Having a problem with the width of a zoomable image element
- react-router-dom `history`: Cannot read property 'push' of undefined
- How to Compare Date time in moment js
- React: What is the core difference between setting a state in a constructor vs. just setting the state within the class?
- Radium doesn't work with React Router IndexLink component
- How to overwrite sass variable of a react library
- How to use hooks to load more data?
- How to pass function as props from functional parent component to child
- React Prop Types vs Typescript
- Problem chrome content security policy react extension manifest
- Correct way to cleanup useEffect with Promise