Regex giving incorrect results - javascript

Working in Javascript attempting to use a regular expression to capture data in a string.
My string appears as this starting with the left bracket
['ABC']['ABC.5']['ABC.5.1']
My goal is to get each piece of the regular expression as a chunk or in array.
I have reviewed and see that the match function might be a good choice.
var myString = "['ABC']['ABC.5']['ABC.5.1']";
myString.match(/\[/g]);
The output I see is only the [ for each element.
I would like the array to be like this for example
myString[0] = ['ABC']
myString[1] = ['ABC.5']
myString[2] = ['ABC.5.1']
What is the correct regular expression and or function to get the above-desired output?

If you just want to separate them, you can use a simple expression or better than that you can split them:
\[\'(.+?)'\]
const regex = /\[\'(.+?)'\]/gm;
const str = `['ABC']['ABC.5']['ABC.5.1']`;
const subst = `['$1']\n`;
// The substituted value will be contained in the result variable
const result = str.replace(regex, subst);
console.log('Substitution result: ', result);
DEMO

You can use this regex with split:
\[[^\]]+
Details
\[ - Matches [
[^\]]+ - Matches anything except ] one or more time
\] - Matches ]
let str = `['ABC']['ABC.5']['ABC.5.1']`
let op = str.split(/(\[[^\]]+\])/).filter(Boolean)
console.log(op)

Related

Remove ’ on string using regex

I have a word like this What’s On. How to remove space and ’?
I can remove space like so data.caption.replace(/ +/g, "") How to do the other part?
You can use [] to provide a character set. So in this case, the following would match against the weird quote and a space.
/[’ ]+/g
This expresion might simply work:
\s*’
which checks for 0 or more spaces prior to ’.
console.log("What ’s On ?".replace(/\s*’/,""));
console.log("What ’s On ?".replace(/[\s’]+/,""));
Or if we wish to replace all spaces:
const regex = /([^\s’]+)|(.+?)/gm;
const str = `What ’s On ?`;
const subst = `$1`;
// The substituted value will be contained in the result variable
const result = str.replace(regex, subst);
console.log(result);
Demo

Regex for picking a Value After “=”

How i can select RQR-1BN6Q360090-0001 (without quotes) using Regex in below -
<html><head><title>Object moved</title></head><body>
<h2>Object moved to here.</h2>
</body></html>
I tried this but it does not work
RptNum=([A-Za-z]+)$
You may use
/RptNum=([\w-]+)/
The pattern will match RptNum= and then capture 1 or more occurrences of word chars (letters, digits and _) or hyphens. See the regex demo and the regex graph:
Note that
/RptNum=([A-Z0-9-]+)/
might be a more restrictive pattern that should work, too. It does not match _ and lowercase letters.
In JS, use it with String#match() and grab the second array item upon a match:
var s = 'Object moved to here';
var m = s.match(/RptNum=([\w-]+)/);
if (m) {
console.log(m[1]);
}
Here, we can also use an expression that collects the new lines, such as:
[\s\S]*RptNum=(.+?)"[\s\S]*
[\w\W]*RptNum=(.+?)"[\w\W]*
[\d\D]*RptNum=(.+?)"[\d\D]*
and our desired output is saved in (.+?).
Test
const regex = /[\s\S]*RptNum=(.+?)"[\s\S]*/gm;
const str = `<html><head><title>Object moved</title></head><body>
<h2>Object moved to here.</h2>
</body></html>`;
const subst = `$1`;
// The substituted value will be contained in the result variable
const result = str.replace(regex, subst);
console.log(result);
Demo
RegEx
If this expression wasn't desired, it can be modified/changed in regex101.com.
RegEx Circuit
jex.im visualizes regular expressions:
const text = 'RptNum=RQR-1BN6Q360090-0001';
console.log(text.match(/RptNum=.*/).map(m => m.match(/RptNum=.*/)[0])[0].split('RptNum=')[1]);
I suppose that works

Plain Javascript Split String UPPER from lower case

i'm trying to split a String which contains an Uppercase Part + a Lowercase Part in Javascript (ES5). The String always looks like "UPPERlower".
Here's what i currently use
"ABCabc".match(/^[A-Z]+/g).concat("ABCabc".match(/[a-z]+$/g)) //result is ["ABC", "abc"]
Is there a cleaner code to achieve this?
EDIT:
Ori Drori's answer and mplungjan's answer are both correct for my Problem.
You can use | to match either the uppercase or lowercase sequence:
var result = "ABCabc".match(/[A-Z]+|[a-z]+/g)
console.log(result);
Destructuring is the modern way
const s="UPPERlower";
let [,upper, lower] =/([A-Z]+)([a-z]+)/g.exec(s);
console.log(upper,lower);
Looping - NOTE: What is the difference between RegExp’s exec() function and String’s match() function?
const s = "UPPERlower UPlow 123 lowUP UPPlowe";
// Note:above will produce low UP using .match(/[A-Z]+|[a-z]+/g) instead of capturing
const re = /([A-Z]+)([a-z]+)/g;
while (match = re.exec(s)) {
[, upper, lower] = match;
console.log(upper, lower)
}
Older way:
var parts=/([A-Z]+)([a-z]+)/g.exec("UPPERlower");
console.log(parts[1],parts[2]);

Regular Expressions in NodeJS: Only split() if next character is not a dot

The following helps me to split a string:
var separators = ['\\\.', '\\\(', '\\\)', ':', '\\\?', '\\\!', '\\\"', '\\\n'];
var tokens = someString.split(new RegExp(separators.join('|'), 'g'));
Whenever there is a . the string is split. However, my document sometimes contains ... and when that is the case, I would like to not to split the string three times, but only after the last dot.
To illustrate, consider the following sentences:
(1) I saw the dog. But I didn't care.
(2) The duck didn't see it coming...
(1) should give me an array with two sentences.
(2) should give me an array with one sentence.
As of right now, (2) would give me an array with 3 sentences, as there are three dots.
Is there any way for me to do this easily?
you can use boundaries \b like this /\b\s*\.\s*\b/g
\s* mean zero or more spaces
\. match the . symbol
Regex Demo
Demo Output
const re = /\b\s*\.\s*\b/g;
const str = ` I saw the dog. But I didn't care.`;
const str2 = `The duck didn't see it coming...`;
const str3 = `This is not....the end. this is. not the beginning Linking Park`;
var found = str.split(re);
var found2 = str2.split(re);
var found3 = str3.split(re);
console.log(found);
console.log(found2);
console.log(found3);
for the string with more than two . use /([.]{2,})/g;
const re = /([.]{2,})/g;
const str = `abc...def.ghi`;
var found = str.match(re);
console.log(found);
after that
you can use the following pattern /([^.]+)/g
example in regex101
const re = /([^.]+)/g;
const str = `abc.def.ghi`;
var found = str.match(re);
console.log(found);

removing affix with javascript in a array

where do we start if we want to remove the affix from this sentence meangan menangkan dimenangkan
affix_list = [
'me-an',
'me-kan,
'di-kan
]
string = 'meangan menangkan dimenangkan'
so it will output
output = [
'ang',
'nang'
'menang'
]
You might want to use regular expressions for those replacements. Starting from your affix_list, this should work:
output = affix_list.reduce(function(str, affix) {
var parts = affix.split("-");
var regex = new RegExp("\\b"+parts[0]+"(\\S+)"+parts[1]+"\\b", "g");
return str.replace(regex, "$1")
}, string).split(" ");
Your regexes will look like this:
/\bme(\S+)an\b/g
/\bme(\S+)kan\b/g
/\bdi(\S+)kan\b/g
But note that you will of course need to replace me-kan before me-an, else "menangkan" will become nangk before the me-kan expression can be applied.
You'll need to start with Javascript regular expressions and iterate through the values, retrieving the middle value accordingly. I'll do that first one for you, and you can try out the rest :)
var re = /me(\w+)an/;
var str = "meangan";
var newstr = str.replace(re, "$1");
console.log(newstr);
// outputs ang
Reference: https://developer.mozilla.org/en/JavaScript/Guide/Regular_Expressions

Categories