1

I want to split when getting , or . in text in javascript.

My text is like this:

The cats climbed the tall tree.In this sentence, $U_SEL{} is a noun.

I want array as:

1.The cats climbed the tall tree.
2.In this sentence
3.$U_SEL{}
4.is a noun
lorond
  • 3,856
  • 2
  • 37
  • 52
Shalini
  • 27
  • 3
  • 1
    why would you split between `$U_SEL{}` and `is a noun.` - there is no , or . there ... `.split(/[.,]/)` – Jaromanda X Aug 05 '16 at 12:17
  • 1
    If you want to isolate $U_SEL{} you need to think about how to identify that part of the string. For example, if you first split on , . and then search for strings that contain a variable that starts with $, and then split that string on spaces.... – Kokodoko Aug 05 '16 at 12:28
  • Possible duplicate of [How do I split a string with multiple separators in javascript?](http://stackoverflow.com/questions/650022/how-do-i-split-a-string-with-multiple-separators-in-javascript) – Paarth Aug 05 '16 at 12:30

6 Answers6

1

The regular expression for this challenge will be.

var text = "The cats climbed the tall tree.In this sentence, $U_SEL{} is a noun."
var regex = /[.,]/;
text.split(regex);

FOR MORE INFORMATION ABOUT regex VISIT https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions

tsadkan yitbarek
  • 1,360
  • 2
  • 11
  • 28
1

Here is the regex. To split on {} first replace that with {}, or {}., then try split.

var str = "The cats climbed the tall tree.In this sentence, $U_SEL{} is a noun";
str = str.replace("{}", "{},");

//Array with splitted value
var result = str.split(/[,.]/g);

//Printing the result array
console.log(result);
Pugazh
  • 9,453
  • 5
  • 33
  • 54
1

try this

<script type="text/javascript">
    var text = "The cats climbed the tall tree.In this sentence, $U_SEL{}, is a noun";
    var spliteds = text.split(/[\.,]/);

    alert(spliteds[0]);
    alert(spliteds[1]);
    alert(spliteds[2]);
    alert(spliteds[3]);
</script>
caldera.sac
  • 4,918
  • 7
  • 37
  • 69
0
 'The cats climbed the tall tree.In this sentence, $U_SEL{} is a noun.'.split(/[\.,]/)

would return:

Array [ "The cats climbed the tall tree", "In this sentence", " $U_SEL{} is a noun", "" ]

Take a look at String.prototype.split()

Dan Schalow
  • 146
  • 12
0

A Regular Expression is your best option in this case. All the above posts are already correctly covering this way of solving your issue. I am just leaving here another approach that will provide what you are after if you have no idea of how Regular Expressions work.

Take into account though that RegExp is quite optimal choice in your scenario. The above code is mostly to show how it can be done without using RegExps. (Not to mention that it will get chaotic adding more delimiters)

var myString = "The cats climbed the tall tree.In this sentence, $U_SEL{} , is a noun";
var mySplitString = myString.split(",");
var myFinalArray = new Array();

mySplitString.forEach(function(part){
  var myTemp = part.split(".");
  myTemp.forEach(function(key){
    myFinalArray.push(key);
  });
});

console.log(myFinalArray);
Strahdvonzar
  • 400
  • 2
  • 10
0

Maybe split is not accurate because splitting requires a single character delimiter and there is no delimiter for the third element.

Trying capturing rather than splitting may work better (though I don't know if it is wise from performance point of view).

You could try this:

var pattern = /(([^.,]+?)([.,]|\{\})) */g;

var captures = [];
var s = 'First capture.Second capture, $THIRD_CAPTURE{} fourth capture.';
while ( (match = pattern.exec(s)) != null ) {
 if (match[3] == "." || match[3] == ",") {
  captures.push(match[2]);
 } else {
  captures.push(match[1]);
 }
}
console.log(captures);

var captures = [];
var s = 'The cats climbed the tall tree.In this sentence, $U_SEL{} is a noun.';
while ( (match = pattern.exec(s)) != null ) {
 if (match[3] == "." || match[3] == ",") {
  captures.push(match[2]);
 } else {
  captures.push(match[1]);
 }
}
console.log(captures);

The principle is as below.

  • Capture blocks of either a part of the sentence ended by a dot or a comma, without inner dot or comma, or ending with empty brackets pair
  • Within each block, capture both the content and the ending (either a dot, a comma or an empty brackets pair)

For each resulting match, you have three captures:

  • At index 1, the first block
  • At index 3, the ending
  • At index 2, the content without the ending

Then, according to the ending, either the match of idx 1 or 2 is stored.

You could modify the loop selecting the match to get exactly what you want, with the dot on the first capture and not on the last one, unless it is a typo.

Gzeh Niert
  • 148
  • 8