0

I am attempting to come up with a regex that captures the following:

var testString = "one . one\.two . one\.two\.three" ;
testString.match(/(\\\.)|([^\.\[\]\s]+)/g  );

or perhaps using double-backslashes:

var testString = "one . one\\.two . one\\.two\\.three" ;
testString.match(/(\\\.)|([^\.\[\]\s]+)/g  );

and would yield:

one

one\.two

one\.two\.three

or even better, eliminate the backslashes:

one

one.two

one.two.three

But I am not sure how to look for a period but yet ignore a period that is trailing a backslash.

In other words, i am trying to manually build an object path. if the path looked something like this:

myObject.one.one_two.one_two_three

it would be easy, but my object level-names have period characters in them.
so I am trying to split up a string by periods that are not backslashed.

i hope this makes sense. thank you all very much.

4
  • 1
    The backslashes in the original string are only present in your notation, not in the actual string. Whether you remove them from the original string notation or not, it is the same string. So there is no way to detect whether you had a backslash in your notation or not. If you want them in your string, you need to double them in your notation. Commented Jan 3, 2017 at 18:32
  • 2
    Is there something wrong with testString.split(/\s+\.\s+/)? Then to remove the backslashes (if they are physical backslashes), .map(x => x.replace("\\", "")? By the way, is there some reason you are referring to backreferences in the title of your post? Also, minor point, but what is the purpose of String() here? Commented Jan 3, 2017 at 18:44
  • As @trincot points out, you have to distinguish between the use of a backslash within the notation for a string literal to escape characters, and actual backslashes in the string. The string literal notation var testString = "one . one\.two . one\.two\.three" ; results in a string one . one.two . one.two.three. The string literal notation var testString = "one . one\\.two . one\\.two\\.three" ; results in a string one . one\.two . one\.two\.three, where the backslashes are actual backslash characters within the string value.. Commented Jan 3, 2017 at 18:56
  • i am modifying existing code so most of this is just a cut/paste - and your map suggestion worked great (very minor tweak): .map(x => x.replace(/\\/g, "")); Commented Jan 3, 2017 at 19:07

1 Answer 1

1

If you intend the original to have literal backslashes in them, you need to escape them in your literal string notation, like this:

var testString = "one . one\\.two . one\\.two\\.three" ;

I will assume this is what you intended.

If the parts are always separated by space-dot-space, it is easier to just split the string by that.

In case this is not suitable for you (your general pattern is not always like that and/or you really need it to be a regular expression), you can use this variant of your regex:

/(\\\.|[^\s\.\\])+/g

var testString = "one . one\\.two . one\\.two\\.three" ;
console.log('input:', testString);

var result = testString.match(/(\\\.|[^\s\.\\])+/g);

// Show result:
for (var match of result) {
    console.log('output:', match);
}

You can remove the backslashes afterwards with match.replace(/\\\./g, '.') in the above code.

Sign up to request clarification or add additional context in comments.

2 Comments

thank you. not to brag, but i was pretty close on my answer. space-dot-space was just there to make it more readable.
Indeed, you were close. ;-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.