Shalini
Shalini

Reputation: 27

spliting text into array in javascript

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

Upvotes: 1

Views: 105

Answers (6)

Pugazh
Pugazh

Reputation: 9561

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);

Upvotes: 1

Gzeh Niert
Gzeh Niert

Reputation: 168

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.

Upvotes: 0

tsadkan yitbarek
tsadkan yitbarek

Reputation: 1370

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

Upvotes: 1

Strahdvonzar
Strahdvonzar

Reputation: 400

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);

Upvotes: 0

caldera.sac
caldera.sac

Reputation: 5088

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>

Upvotes: 1

Dan Schalow
Dan Schalow

Reputation: 166

 '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()

Upvotes: 0

Related Questions