2

I have a function that takes a string as an argument, my problem is that the string can be a sentence such as: "My name is John Doe. I need a car." or it can be a dot notation: "this.is.dot.notation". I don't know much about regex (if that's what I need) but I would like to separate the two types.

Is there a way to check if a string is in Object/Dot notation or not, assuming that the string can also be a word or a sentence? From https://stackoverflow.com/a/2390793/1251031, I thought of this, but this would not work with a sentence that may contain more than one period correct? Should regex be used?

if(input.split('.').length > 1){

}

Later, the dot notation will be used to access Object properties as seen here: Convert string in dot notation to get the object reference

Machavity
  • 30,841
  • 27
  • 92
  • 100
iwatakeshi
  • 697
  • 3
  • 17
  • 31
  • 2
    please explain what problem you're actually trying to solve that makes you think you need to detect full stops/periods in strings, to prevent us from heading off into an [XY problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) – Mike 'Pomax' Kamermans Sep 12 '14 at 01:13
  • @Mike'Pomax'Kamermans Ah, sorry. I have a function that takes a string as an argument, my problem is that the string can be a sentence such as: "My name is John Doe. I need a car." or it can be a dot notation: "this.is.dot.notation". I don't know much about regex (if that's what I need) but I would like to separate the two types. – iwatakeshi Sep 12 '14 at 01:18
  • please add all that to your question instead, so others can understand what you need without having to read the comment thread =) That said, your dot notation example doesn't seem very meaningful, do you have a concrete example of what that input should even do? – Mike 'Pomax' Kamermans Sep 12 '14 at 01:19
  • nice, but you still need to explain what that "dot notation" input is supposed to do. – Mike 'Pomax' Kamermans Sep 12 '14 at 01:22
  • @Mike'Pomax'Kamermans Any more details needed? – iwatakeshi Sep 12 '14 at 01:27
  • yeah, at this point I have to question why your function is letting users call your code. What problem is your function solving that it needs to give access to property values AND do double duty as normal text processing function. Don't overload it that way, use two separate functions. – Mike 'Pomax' Kamermans Sep 12 '14 at 01:29
  • Test if the string contains more than one dot and no spaces. – Barmar Sep 12 '14 at 01:30
  • @Mike'Pomax'Kamermans Yeah, I am trying to avoid posting a great deal of code but essentially, the function will be an n-argument function. `argument[0]` will be for string input and `n>1` would be used for sprintf like purposes. Specifically, I created an [i18n library](https://github.com/iwatakeshi/gengojs), but it's rather limited when handling Objects (JSON like). I need to use `argument[0]` for dot notation and words/senteces and be able to differentiate from the two. – iwatakeshi Sep 12 '14 at 01:36
  • I can't say that's good practice. Making it accept a string or an object is fine (`function(in) { if (typeof in === "string") { ... } else if (typeof in === "object") { ... } }` but testing the string input to see what pattern it matches and then actually pretending it's API traversal is really bad practice, and potentially library breaking. – Mike 'Pomax' Kamermans Sep 12 '14 at 01:39

2 Answers2

2

Test if the string contains at least 2 dots, but no spaces. That seems to be the definition of dot notation.

if (str.match(/\..*\./) && !str.match(/\s/)) {
    // dot notation
} else {
    // sentence
}

Or maybe it's a dot that isn't at the end of the string, and no whitespace:

if (str.match(/\.[^.]/) && !str.match(/\s)) {
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • 1
    "a.b" seems a dot notation, but it contains only one dots. 2 dots are really necessary? – Fumu 7 Sep 12 '14 at 01:47
  • I was just guessing at what he considers dot notation. I didn't want a sentence like `Hello.` to be considered dot notation. Maybe its a dot with something before and after it? – Barmar Sep 12 '14 at 01:49
  • @Barmar The second one worked. Also I've tried `test()` but gave me issues so I used `match()` instead. – iwatakeshi Sep 12 '14 at 01:59
  • Yeah, I think it's actually `/regexp/.test(str)`, not `str.test(/regexp/)`. – Barmar Sep 12 '14 at 02:00
0

This just checks if one or more word characters is followed by a dot that is followed by one or more word characters with no space inbetween. If you just need a boolean you can remove the ternary ( the question mark and everything following it (keep the semi-colon of course) ).

function isDotNotation(str) {
  return (/[\w+]\.[\w+]/gi).test(str) ? "Dot Notation" : "Sentence";
}

This would break it: isDotNotation("Hell o.there");

So here's an uglier one that checks for one or more whitespace/s.

function isDotNotation(str) {
  return ( (/[\w+]\.[\w+]/gi).test(str) ) && ( !(/\s+/g).test(str) ) ? "Dot Notation" : "Sentence";
}
Cory Z A
  • 74
  • 3