match until an unescaped version of a character - javascript

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

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.

Remove text between square brackets at the end of string

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, ''));

How to match string inside second set of brackets with Regex Javascript

Here is my string:
type_logistics[][delivery]
type_logistics[][random]
type_logistics[][word]
I would like to pull out the word, whatever it is, inside the second set of brackets. I thought that meant doing something like this:
Indicate that the start of the string I want to capture is [ by writing ^\[
Indicate that there will be any number 1+ of characters using [a-z]+
Indicate that the end will be ] by using \]$
The above three steps should get me to [delivery], [random], [word] in which case I'd just wrap the entire regex in a capture parenthesis ()
My finished statement would have been
string.match(/^\[([a-z]+)\]$/)
Have been playing with regex101.com and literally none of my assumptions have worked LOL. Please help?
With ^ you are assuming the String you are checking starts there. Your String starts with type_logistics and not as expected by the regex with a [
To detect the 2nd set of brackets you need to either add the type_logistics[] to the regex or just match everything before the 1st set of brackets with .*
When working with multiple lines (for example during testing on regex101), don't forget to set the modifiers gm
g modifier: global. All matches (don't return on first match) m modifier: multi-line. Causes ^ and $ to match the begin/end of each
line (not only begin/end of string)
These all would work for your test cases
/^.*\[\]\[([a-z]+)\]$/gm
/^type_logistics\[\]\[([a-z]+)\]$/gm
/^.*\[([a-z]+)\]$/gm
Match [ followed by a-z followed by ] , convert back to string, split [ character, filter "" empty string
var str = "type_logistics[][delivery] type_logistics[][random] type_logistics[][word]"
var res = str.match(/(\[[a-z]+)(?=\])/g).join("").split(/\[/).filter(Boolean);
console.log(res);
document.body.textContent = res;

JS regexp to match special characters

I'm trying to find a JavaScript regexp for this string: ![](). It needs to be an exact match, though, so:
`!()[]` // No match
hello!()[] // No match
!()[]hello // No Match
!()[] // Match
!()[] // Match (with a whitespace before and/or after)
I tried this: \b![]()\b. It works for words, like \bhello\b, but not for those characters.
The characters specified are control characters and need to be escaped also user \s if you want to match whitespace. Try the following
\s?!(?:\[\]\(\)|\(\)\[\])\s?
EDIT: Added a capture group to extract ![]() if needed
EDIT2: I missed that you wanted order independant for [] and () I've added it in this fiddle http://jsfiddle.net/MfFAd/3/
This matches your example:
\s*!\[\]\(\)\s*
Though the match also includes the spaces before and after !()[].
I think \b does not work here because ![]() is not a word. Check out this quote from MDN:
\b - Matches a word boundary. A word boundary matches the position where a word character is not followed or preceeded by another word-character. Note that a matched word boundary is not included in the match. In other words, the length of a matched word boundary is zero.
Let's create a function for convenience :
function find(r, s) {
return (s.match(r) || []).slice(-1);
}
The following regular expression accepts only the searched string and whitespaces :
var r = /^\s*(!\[\]\(\))\s*$/;
find(r, '![]() '); // ["![]()"]
find(r, '!()[] '); // []
find(r, 'hello ![]()'); // []
This one searches a sub-string surrounded by whitespaces or string boundaries :
var r = /(?:^|\s)(!\[\]\(\))(?:\s|$)/;
find(r, '![]() '); // ["![]()"]
find(r, 'hello ![]()'); // ["![]()"]
find(r, 'hello![]()'); // []
To match all characters except letters and numbers you can use this regex
/[^A-Z0-9]/gi
g - search global [ mean whole text, not just first match ]
i -case insensitive
to remove any other sign for example . and ,
/[^A-Z0-9\.\,]/gi
In order to match exact string you need to group it and global parameter
/(\!\[\]\(\))/g
so it will search for all matches

Regex (regular expressions), replace the second occurence in javascript

This is an example of the string that's being worked with:
xxxxxx[xxxxxx][7][xxxxxx][9][xxxxxx]
I'm having a little trouble matching the second occurrence of a match, I want to return the 2nd square brackets with a number inside. I have some regex finding the first square backets with numbers in a string:
\[+[0-9]+\]
This returns [7], however I want to return [9].
I'm using Javascript's replace function, the following regex matches the second occurrence (the [9]) in regex testeing apps, however it isn't replaced correctly in the Javascript replace function:
(?:.*?(\[+[0-9]+\])){2}
My question is how do I use the above regex to replace the [9] in Javasctipt or is there another regex that matches the second occurrence of a number in square brackets.
Cheers!
If xxx is just any string, and not necessarily a number, then this might be what you want:
(\[[0-9]+\]\[.*?\])\[([0-9]+)\]
This looks for the second number in []. Replace it with $1[<replacement>]. Play with it on rubular.
Your regular expression fails to work as intended because groups followed by + only end up holding the last [xxx].
Try
result = subject.replace(/(\[\d\]\[[^\]]+\])\[\d\]/, "$1[replace]");
As a commented regex:
( # capture the following in backref 1:
\[\d\] # first occurrence of [digit]
\[ # [
[^\]]+ # any sequence of characters except ]
\] # ]
) # end of capturing group
\[\d\] # match the second occurence of [digit]
If the number of [xxx] groups between the first and second [digit] group is variable, then use
result = subject.replace(/(\[\d\](?:\[[^\]]+\])*?)\[\d\]/, "$1[replace]");
By surrounding the part that matches the [xxx] groups with (non-capturing) parentheses and the lazy quantifier *? I'm asking the regex engine to match as few of those groups as possible, but as many as necessary so the next group is a [digit] group.
console.log( "xxxxxx[xxxxxx][7][xxxxxx][9][xxxxxx]".replace(
/^(.*\[[0-9]+\].*)(\[[0-9]+\])(.*)$/,
'$1[15]$3')); // replace with matches before ($1) and after ($3) your match ($2)
returns:
// xxxxxx[xxxxxx][7][xxxxxx][15][xxxxxx]
It will match where [n] is preceeded by 1 set of brackets with numbers inside.

Categories

Resources