# How to get substring in js in a regular season?

Good afternoon.
There is a line:

"Organix (dry food) for dogs of small breeds with chicken, Adult Dog Small Breed Chicken (12kg)"

It is necessary to obtain two lines.
The first line.
The first line should meet the following conditions:
• Line contained in parentheses "(" and ")"
• The string contains numbers

we should get this line:

"12 kg"

The second line.
The second line should meet the following condition:
It should not contain the string in brackets (including the brackets) which contains numbers.
we should get this line:

"Organix (dry food) for dogs of small breeds with chicken, Adult Dog Small Breed Chicken "
April 19th 20 at 12:47
April 19th 20 at 12:49
Solution
``````function getStrings(str) {
const str1 = str.match(/\(.+?\)/g)?.find(n => /\d/.test(n)) || ";
return {
str1: str1.slice(1, -1),
str2: str.replace(str1, "),
};
}``````
The first line in the function returns correctly:
For the line:
Organix (dry food) for dogs of small breeds with chicken, Adult Dog Small Breed Chicken (800 g)
the function does not return
"800 g"
- garnet_Sta commented on April 19th 20 at 12:52
@lilla.Gutmann, returns. Disagree - care to prove your words, show exactly what you are doing. - amani.Glov commented on April 19th 20 at 12:55
Sorry. Found a mistake myself. Thank you, you solved the problem flawlessly! - garnet_Sta commented on April 19th 20 at 12:58
April 19th 20 at 12:51
``````splitOnTwoParts function(str) {
const regexp = /\([^\(\)]*\d[^\(\)]*\)/;
const innerStr = str.match(regexp)[0];
const firstPart = innerStr.slice(1, -1);
const secondPart = str.replace(innerStr, ");
return [firstPart, secondPart];
}

splitOnTwoParts("Organix (dry food) for dogs of small breeds with chicken, Adult Dog Small Breed Chicken (12kg)");
// ["12 kg", "Organix (dry food) for dogs of small breeds with chicken, Adult Dog Small Breed Chicken "]``````

Find more questions by tags Regular expressionsJavaScript