Javascript regular expression between brackets - javascript

Let's say in the following text
I want [this]. I want [this too]. I don't want \[this]
I want the contents of anything between [] but not \[]. How would I go about doing that? So far I've got /\[([^\]]+)\]/gi. but it matched everything.

Use this one: /(?:^|[^\\])\[(.*?)\]/gi
Here's a working example: http://regexr.com/3clja
?: Non-capturing group
^|[^\\] Beggining of string or anything but \
\[(.*?)\] Match anything between []
Here's a snippet:
var string = "[this i want]I want [this]. I want [this too]. I don't want \\[no]";
var regex = /(?:^|[^\\])\[(.*?)\]/gi;
var match = null;
document.write(string + "<br/><br/><b>Matches</b>:<br/> ");
while(match = regex.exec(string)){
document.write(match[1] + "<br/>");
}

Use this regexp, which first matches the \[] version (but doesn't capture it, thereby "throwing it away"), then the [] cases, capturing what's inside:
var r = /\\\[.*?\]|\[(.*?)\]/g;
^^^^^^^^^ MATCH \[this]
^^^^^^^^^ MATCH [this]
Loop with exec to get all the matches:
while(match = r.exec(str)){
console.log(match[1]);
}

/(?:[^\\]|^)\[([^\]]*)/g
The content is in the first capture group, $1
(?:^|[^\\]) matches the beginning of a line or anything that's not a slash, non-capturing.
\[ matches a open bracket.
([^\]]*) captures any number of consecutive characters that are not closed brackets
\] matches a closing bracket

Related

Validate text with javascript RegEX

I'm trying to validate text with javascript but can find out why it's not working.
I have been using : https://regex101.com/ for testing where it works but in my script it fails
var check = "test"
var pattern = new RegExp('^(?!\.)[a-zA-Z0-9._-]+$(?<!\.)','gmi');
if (!pattern.test(check)) validate_check = false;else validate_check = true;
What i'm looking for is first and last char not a dot, and string may contain [a-zA-Z0-9._-]
But the above check always fails even on the word : test
+$(?<!\.) is invalid in your RegEx
$ will match the end of the text or line (with the m flag)
Negative lookbehind → (?<!Y)X will match X, but only if Y is not before it
What about more simpler RegEx?
var checks = ["test", "1-t.e_s.t0", ".test", "test.", ".test."];
checks.forEach(check => {
var pattern = new RegExp('^[^.][a-zA-Z0-9\._-]+[^.]$','gmi');
console.log(check, pattern.test(check))
});
Your code should look like this:
var check = "test";
var pattern = new RegExp('^[^.][a-zA-Z0-9\._-]+[^.]$','gmi');
var validate_check = pattern.test(check);
console.log(validate_check);
A few notes about the pattern:
You are using the RegExp constructor, where you have to double escape the backslash. In this case with a single backslash, the pattern is ^(?!.)[a-zA-Z0-9._-]+$(?<!.) and the first negative lookahead will make the pattern fail if there is a character other than a newline to the right, that is why it does not match test
If you use the /i flag for a case insensitive match, you can shorten [A-Za-z] to just one of the ranges like [a-z] or use \w to match a word character like in your character class
This part (?<!\.) using a negative lookbehind is not invalid in your pattern, but is is not always supported
For your requirements, you don't have to use lookarounds. If you also want to allow a single char, you can use:
^[\w-]+(?:[\w.-]*[\w-])?$
^ Start of string
[\w-]+ Match 1+ occurrences of a word character or -
(?: Non capture group
[\w.-]*[\w-] Match optional word chars, a dot or hyphen
)? Close non capture group and make it optional
$ End of string
Regex demo
const regex = /^[\w-]+(?:[\w.-]*[\w-])?$/;
["test", "abc....abc", "a", ".test", "test."]
.forEach((s) =>
console.log(`${s} --> ${regex.test(s)}`)
);

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]";

Select everything between not starting with but ending with

So I am trying to select some names with JS but I can figure out how. I found 3 solution here but still could not get it to work:
I would like to select word that DOESN'T start with . and HAS to end with {
Here is what I have:
\b(?!\.)[\w\-]+(?=\s*{)\b
Also tried: ^(?!\.)[\w\-]+(?=\s*:)
Example:
.test { }
test { } <--- Select this test
If you wish to match -^!foo {}, use (?:\s|^)([^\s\.]+(?=\s*\{)).
If you wish to only match foo {}, use (?:[^\w\.]|^)([^\W\.]+(?=\s*\{)).
var pattern1 = /(?:\s|^)([^\s\.]+(?=\s*\{))/gm,
pattern2 = /(?:[^\w\.]|^)([^\W\.]+(?=\s*\{))/gm,
text = ".foo{} bar {} !!baz{} ..-boom {}",
match;
console.log('First pattern:');
while (match = pattern1.exec(text)) {
console.log(match[1]); // Prints "bar", "!!baz"
}
console.log('Second pattern:');
while (match = pattern2.exec(text)) {
console.log(match[1]); // Prints "bar", "baz", "boom"
}
Explanation of the first regex:
We expect the leading position before your word to either be the start
of the line ^ or whitespace \s.
The word itself consists of repeated non-whitespace characters that
are not dots [^\s\.].
The word must be followed by a {, for which we use lookahead
via (?=\{).
JavaScript's regex engine doesn't support lookbehind, so you have to use a non-capturing group (?:...) to match the leading position before your word.
See JavaScript regular expressions and sub-matches for an explanation of how to access capturing groups
See https://regex101.com/r/bT8sE5/5 for a live demo of the regex with further explanation.
How about this:
([^\w\.]|^)(\w+\{)
It's basically saying anything at the start of the line, or beginning with a nonword / nondot character.
It's tricky to do with \b since it matches after the dot quite happily. You can possibly get it to work with the negative lookahead but it's pretty funky stuff at this point :)
You can do it with this: ^.*\.(\w+\{\}){1}.*$
Explanation:
^ is the beginning of the string
.* matches everything behind the dot (.)
(\w+\{\})* the capture group matches the word and the brackets after it (for example test{}} zero or more times
.* matches everything after the word
$ is the end of the string
So for the input: sadasdas.test{}daasdasdasdasd it will match test{}
Try it out here: https://regex101.com/r/hE4uY4/1
The following works in relation to http://regexr.com/
You can test it there.
/(?![\s])(^[^.]([\S]+)[{}][\s])/igm

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

Javascript regular expression matching prior and trailing characters

I have this string in a object:
<FLD>dsfgsdfgdsfg;NEW-7db5-32a8-c907-82cd82206788</FLD><FLD>dsfgsdfgsd;NEW-480e-e87c-75dc-d70cd731c664</FLD><FLD>dfsgsdfgdfsgfd;NEW-0aad-440a-629c-3e8f7eda4632</FLD>
this.model.get('value_long').match(/[<FLD>\w+;](NEW[-|\d|\w]+)[</FLD>]/g)
Returns:
[";NEW-7db5-32a8-c907-82cd82206788<", ";NEW-480e-e87c-75dc-d70cd731c664<", ";NEW-0aad-440a-629c-3e8f7eda4632<"]
What is wrong with my regular expression that it is picking up the preceding ; and trailing <
here is a link to the regex
http://regexr.com?30k3m
Updated:
this is what I would like returned:
["NEW-7db5-32a8-c907-82cd82206788", "NEW-480e-e87c-75dc-d70cd731c664", "NEW-0aad-440a-629c-3e8f7eda4632"]
here is a JSfiddle for it
http://jsfiddle.net/mwagner72/HHMLK/
Square brackets create a character class, which you do not want here, try changing your regex to the following:
<FLD>\w+;(NEW[-\d\w]+)</FLD>
Since it looks like you want to grab the capture group from each match, you can use the following code to construct an array with the capture group in it:
var regex = /<FLD>\w+;(NEW[\-\d\w]+)<\/FLD>/g;
var match = regex.exec(string);
var matches = [];
while (match !== null) {
matches.push(match[1]);
match = regex.exec(string);
}
[<FLD>\w+;] would match one of the characters inside of the square brackets, when I think what you actually want to do is match all of those. Also for the other character class, [-|\d|\w], you can remove the | because it is already implied in a character class, | should only be used for alternation inside of a group.
Here is an updated link with the new regex: http://jsfiddle.net/RTkzx/1

Categories

Resources