Remove text between square brackets at the end of string - javascript

I need a regex to remove last expression between brackets (also with brackets)
source: input[something][something2]
target: input[something]
I've tried this, but it removes all two:
"input[something][something2]".replace(/\[.*?\]/g, '');

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(/\[[^\][]*]$/, '')
);
Details:
\[ - a literal [
[^\][]* - zero or more chars other than [ and ]
] - a literal ]
$ - end of string
Another way is to use .* at the start of the pattern to grab the whole line, capture it, and the let it backtrack to get the last [...]:
console.log(
"input[something][something2]".replace(/^(.*)\[.*]$/, '$1')
);
Here, $1 is the backreference to the value captured with (.*) subpattern. However, it will work a bit differently, since it will return all up to the last [ in the string, and then all after that [ including the bracket will get removed.

Do not use the g modifier, and use the $ anchor:
"input[something][something2]".replace(/\[[^\]]*\]$/, '');

try this code
var str = "Hello, this is Mike (example)";
alert(str.replace(/\s*\(.*?\)\s*/g, ''));

Related

A regexp in Javascript that will capture a word only if it is NOT inside brackets [duplicate]

Here is the example string which I have to match:
var sampleStr = "aaa[bbb=55,zzz=ddd],#ddd[ppp=33,kk=77,rr=fff],tt,ff";
I need to write regex that will match all , characters which is not inside [ ]
so In my sample string I should receive the next , characters:
- `,` before `#ddd`
- `,` before `tt`
- `,` before `ff`
and it should ignore next ,:
- `,` before `zzz`
- `,` before `kk`
- `,` before `rr`
Actually I have no idea how to ignore those , inside [...].
Big thx for any advance
If you can assume that the part inside [] doesn't contain nested [], and the [] are balanced:
var out = content.split(/,(?![^\[\]]*\])/);
(?![^\[\]]*\]) is a negative look-ahead which checks that we are not inside [] with a heuristic. As long as we don't encounter any ] as we consume characters other than [ and ], then we are outside [].
The code above will split the text along those commas , outside brackets [] and return the tokens.
This regex should work
,(?![^\[]*?\])
see: DEMO
Explanation
, is our target comma,
(?![^\[]*?\]) use negative lookahead to guarantee that there is no ] after ,, a trick here is instead of using .* we use [^\[]* to prevent regex match a pattern [...] instead of ..].
One way to avoid commas enclosed in square brackets is to match square brackets first. Example for a replacement:
var result = sampleStr.replace(/([^\[,]*(?:\[[^\]]*\][^\[,]*)*),/g, '$1#');
Other example if you want to split:
var result = sampleStr.match(/(?=[^,])[^\[,]*(?:\[[^\]]*\][^\[,]*)*/g);
The advantage of these approaches is that you don't need to parse all the string until the end with a lookahead for each comma.

match until an unescaped version of a character

Am processing a string format like [enclosed str]outer str[enclosed str]
and am trying to match all [enclosed str].
The problem is that I want any character except an unescaped version of ](that is a ] not preceded by a \) to be within the square brackets.
For instance
str = 'string[[enclosed1\\]]string[enclosed2]';
// match all [ followed by anything other ] then a ]
str.match(/\[[^\]]+]/g)
// returns ["[[enclosed1\]", "[enclosed2]"]
// ignores the `]` after `\\]`
// match word and non-word char enclosed by []
str.match(/\[[\w\W]+]/g)
// returns ["[[enclosed1\]]string[enclosed2]"]
// matches to the last ]
// making it less greedy with /\[[\w\W]+?]/g
// returns same result as /\[[^\]]+]/g
Is it possible within Javascript RegExp to achieve my desired result which is
["[[enclosed1\]]", "[enclosed2]"]
With regex in javascript not supporting a negative lookbehind this is the best I could come up with:
/(?:^|[^\\])(\[.*?[^\\]\])/g
group 1 will contain the string you want.
https://regex101.com/r/PmDcGH/3

How to replace part of a string using regex

i need to replace a part of a string in Javascript
The following example should clarify what i mean
var str = "asd[595442/A][30327][0]";
var strToReplace = "30333";
var strDesiredResult = "asd[595442/A][30333][0]";
Basically it means the second area within the brackets should get replaced with another string
How to do that?
What i did so far is something like this :
var str = "asd[595442/A][30327][0]";
var regex = /asd\[(.*)\]\[(.*)\]\[(.*)\]/;
var arrMatches = regex.exec(str);
The string appears in arrMatches[2] correctly, and i could replace this. But what happens if in arrMatches[1] is the same string ?
Because it should only replace the value in the second bracket area.
You may use a regex that will match the first [....] followed with [ and capture that part into a group (that you will be able to refer to via a backreference), and then match 1+ chars other than ] to replace them with your replacement:
var str = "asd[595442/A][30327][0]";
var strToReplace = "30333";
console.log(str.replace(/(\[[^\]]*]\[)[^\]]*/, "$1" + strToReplace));
var strDesiredResult = "asd[595442/A][30333][0]";
console.log(strDesiredResult);
The /(\[[^\]]*]\[)[^\]]*/ has no gmodifier, it will be looking for one match only.
Since regex engine searches a string for a match from left to right, you will get the first match from the left.
The \[[^\]]*]\[ matches [, then any 0+ chars other than ] and then ][. The (...) forms a capturing group #1, it will remember the value that you will be able to get into the replacement with $1 backreference. [^\]]* matches 0+ chars other than ] and this will be replaced.
Details:
( - a capturing group start
\[ - a literal [ symbol (if unescaped, it starts a character class)
[^\]]* - a negated character class that matches zero or more (due to the * quantifier)
] - a literal ] (outside a character class, it does not have to be escaped)
\[ - a literal [
) - end of capturing group #1 (its value can be accessed with $1 backreference from the replacement pattern)
[^\]]* - 0+ (as the * quantifier matches zero or more occurrences, replace with + if you need to only match where there is 1 or more occurrences) chars other than ] (inside a character class in JS regex, ] must be escaped in any position).
Use this pattern:
'asd[595442/A][30327][0]'.replace(/^(asd\[[^\[\]]+\]\[)([^\[\]]+)(\]\[0\])$/, '$130333$3')
Test here
^ - match beginning of string
first group - match "asd[", any chars except [ and ], "]["
second group - match any chars except [ and ]
third group - match exactly: "][0]"
$ - match end of string
There are many ways to do this. One possible pattern is
str.replace(/^(.+)(\[.+\])(\[.+\])(\[.+\])$/, `$1$2[${strToReplace}]$4`)
You can see that $<number> is referred to captured string from regex (string groups in parentheses). We can refer to those and rearrange it however we want.
You can use Regular Expression like this /\[[0-9]+\]/ as below.
var str = "asd[595442/A][30327][0]";
var strToReplace = "30333";
var strDesiredResult = str.replace(/\[[0-9]+\]/, '[' + strToReplace + ']');
console.log(strDesiredResult); //"asd[595442/A][30333][0]";

regex match all `,` not inside \[\]

Here is the example string which I have to match:
var sampleStr = "aaa[bbb=55,zzz=ddd],#ddd[ppp=33,kk=77,rr=fff],tt,ff";
I need to write regex that will match all , characters which is not inside [ ]
so In my sample string I should receive the next , characters:
- `,` before `#ddd`
- `,` before `tt`
- `,` before `ff`
and it should ignore next ,:
- `,` before `zzz`
- `,` before `kk`
- `,` before `rr`
Actually I have no idea how to ignore those , inside [...].
Big thx for any advance
If you can assume that the part inside [] doesn't contain nested [], and the [] are balanced:
var out = content.split(/,(?![^\[\]]*\])/);
(?![^\[\]]*\]) is a negative look-ahead which checks that we are not inside [] with a heuristic. As long as we don't encounter any ] as we consume characters other than [ and ], then we are outside [].
The code above will split the text along those commas , outside brackets [] and return the tokens.
This regex should work
,(?![^\[]*?\])
see: DEMO
Explanation
, is our target comma,
(?![^\[]*?\]) use negative lookahead to guarantee that there is no ] after ,, a trick here is instead of using .* we use [^\[]* to prevent regex match a pattern [...] instead of ..].
One way to avoid commas enclosed in square brackets is to match square brackets first. Example for a replacement:
var result = sampleStr.replace(/([^\[,]*(?:\[[^\]]*\][^\[,]*)*),/g, '$1#');
Other example if you want to split:
var result = sampleStr.match(/(?=[^,])[^\[,]*(?:\[[^\]]*\][^\[,]*)*/g);
The advantage of these approaches is that you don't need to parse all the string until the end with a lookahead for each comma.

Javascript - return string between square brackets

I need to return just the text contained within square brackets in a string. I have the following regex, but this also returns the square brackets:
var matched = mystring.match("\\[.*]");
A string will only ever contain one set of square brackets, e.g.:
Some text with [some important info]
I want matched to contain 'some important info', rather than the '[some important info]' I currently get.
Use grouping. I've added a ? to make the matching "ungreedy", as this is probably what you want.
var matches = mystring.match(/\[(.*?)\]/);
if (matches) {
var submatch = matches[1];
}
Since javascript doesn't support captures, you have to hack around it. Consider this alternative which takes the opposite approach. Rather that capture what is inside the brackets, remove what's outside of them. Since there will only ever be one set of brackets, it should work just fine. I usually use this technique for stripping leading and trailing whitespace.
mystring.replace( /(^.*\[|\].*$)/g, '' );
To match any text in between two adjacent open and close square brackets, you can use the following pattern:
\[([^\][]*)]
(?<=\[)[^\][]*(?=])
See the regex demo #1 and regex demo #2. NOTE: The second regex with lookarounds is supported in JavaScript environments that are ECMAScript 2018 compliant. In case older environments need to be supported, use the first regex with a capturing group.
Details:
(?<=\[) - a positive lookbehind that matches a location that is immediately preceded with a [ char (i.e. this requires a [ char to occur immediately to the left of the current position)
[^\][]* - zero or more (*) chars other than [ and ] (note that ([^\][]*) version is the same pattern captured into a capturing group with ID 1)
(?=]) - a positive lookahead that matches a location that is immediately followed with a ] char (i.e. this requires a ] char to occur immediately to the right of the current regex index location).
Now, in code, you can use the following:
const text = "[Some text] ][with[ [some important info]";
console.log( text.match(/(?<=\[)[^\][]*(?=])/g) );
console.log( Array.from(text.matchAll(/\[([^\][]*)]/g), x => x[1]) );
// Both return ["Some text", "some important info"]
Here is a legacy way to extract captured substrings using RegExp#exec in a loop:
var text = "[Some text] ][with[ [some important info]";
var regex = /\[([^\][]*)]/g;
var results=[], m;
while ( m = regex.exec(text) ) {
results.push(m[1]);
}
console.log( results );
Did you try capturing parens:
("\\[(.*)]");
This should return the pattern within the brackets as a captured match in the returned array
Just use replace and map
"blabla (some info) blabla".match(/\((.*?)\)/g).map(b=>b.replace(/\(|(.*?)\)/g,"$1"))
You can't. Javascript doesn't support lookbehinds.
You'll have to either use a capture group or trim off the brackets.
By the way, you probably don't want a greedy .* in your regex. Try this:
"\\[.*?]"
Or better, this:
"\\[[^\\]]*]"

Categories

Resources