0

Input should be like this:

class1{name=‘adam.smith’}.class2{name=‘john’}.subjectMath

Output something like:

["class1{name='adam.smith'}", "class2{name='john'}", "subjectMath"]

Any solution?

George
  • 6,630
  • 2
  • 29
  • 36
Rajkumar Bansal
  • 323
  • 2
  • 10

4 Answers4

3

Try using split:

var input = "class1{name=‘adam.smith’}.class2{name=‘john’}.subjectMath";
var parts = input.split(/\.(?![^{]*’)/);
console.log(parts);

The regex used for the split requires some explanation:

\.          match a literal dot
(?![^{]*’)  but assert that we DON'T encounter a text curly quote
            looking forward so long as we don't hit a { first

The negative lookahead fails the dot in adam.smith, because we can find a curly quote without encountering a { opening bracket, which would imply that the dot is not a connecting dot.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
0

Giving a sting like this:

string = "class1{name=‘adam.smith’}.class2{name=‘john’}.subjectMath";

You could try with this:

string.split(/(?<=})./)

which will return:

[ "class1{name=‘adam.smith’}" , "class2{name=‘john’}" , "subjectMath" ]
Jazzpaths
  • 645
  • 5
  • 9
0

Swift 5.1

This is a solution for Swift assuming that the input structure remains the same, with the bracket preceding the dot :

var input = "class1{name=‘adam.smith’}.class2{name=‘john’}.subjectMath"

func splittingClasses(_ input: String) -> [String] {
    var total: [String] = []
    var parts = input.components(separatedBy: "}.")
    let endpart =  "}"
    for i in 0 ..< parts.count {
        if i == parts.count - 1 {
            total.append(parts[i])
        } else {
            total.append(parts[i] + endpart)
        }
    }
    print(total)
    return total
}
splittingClasses(input) 
// returns ["class1{name=‘adam.smith’}", "class2{name=‘john’}", "subjectMath"]

multitudes
  • 2,898
  • 2
  • 22
  • 29
0

You could match everything that is not an . of course this gives some issues with the . within ‘...’ context. For this reason we should also match those.

const input = "class1{name=‘adam.smith’}.class2{name=‘john’}.subjectMath";
const output = input.match(/(?:‘[^’]*’|[^.])+/g);

console.log(output);
‘[^’]*’

Will match any and keeps matching until it finds the closing character, matching anything in-between.

[^.]

Will match anything that is not a . character.

3limin4t0r
  • 19,353
  • 2
  • 31
  • 52