splitting string is returning full string - javascript

I want to create a small script which determines how many words are in a paragraph and then divides the paragraph depending on a certain length. My approach was to split the paragraph using split(), find out how many elements are in the array and then output some of the elements into one paragraph and the rest into another.
var para = document.getElementById('aboutParagraph').innerHTML;
var paraElements = para.split();
var paraLength = paraElements.length;
if(paraLength >= 500){
}
console.log(paraElements);
when I use this code paraElements is being returned in an array where the first element is the entire string.
Sof for example if the paragraph were "this is a paragraph" paraElements is being returned as: ["this is a paragraph"], with a a length of 1. Shouldn't it be ["this", "is", "a", "paragraph"]?

var str = "this is a paragraph";
var ans = str.split(' ');
console.log(ans);
You need to use split(' ') with this format. Use ' ', notice space there. You were not passing any parameter by which to split.

The split() method splits a string at a delimiter you specify (can be a literal string, a reference to a string or a regular expression) and then returns an array of all the parts. If you want just one part, you must pass the resulting array an index.
You are not supplying a delimiter to split on, so you are getting the entire string back.
var s = "This is my test string";
var result = s.split(/\s+/); // Split everywhere there is one or more spaces
console.log(result); // The entire resulting array
console.log("There are " + result.length + " words in the string.");
console.log("The first word is: " + result[0]); // Just the first word

You are missing the split delimiter for a space. Try this:
var para = document.getElementById('aboutParagraph').innerHTML;
var paraElements = para.split(' ');
var paraLength = paraElements.length;
if(paraLength >= 500){
}
console.log(paraElements);

The split() will return an array, if you don't pass in a delimiter as an argument it would encapsulate the whole string into one element of an array.
You can break your words on spaces, but you may want to also consider tabs and newlines. For that reason, you could use some regex /\s+/ which will match on any whitespace character.
The + is used so that it treats all consecutive whitespace characters as one delimiter. Otherwise a string with two spaces, like foo bar would be treated as three words with one being an empty string ["foo", "", "bar"] (the plus makes it ["foo", "bar"] as expected).
var para = document.getElementById('aboutParagraph').innerHTML;
var paraElements = para.split(/\s+/); // <-- need to pass in delimiter to split on
var paraLength = paraElements.length;
if (paraLength >= 500) {}
console.log(paraLength, paraElements);
<p id="aboutParagraph">I want to create a small script which determines how many words are in a paragraph and then divides the paragraph depending on a certain length. My approach was to split the paragraph using split(), find out how many elements are in the array and then output some of the elements into one paragraph and the rest into another.</p>

Related

Making a javascript function that finds the first word in a string/sentence

I've looked all over the web but couldnt find a good answer to this. I need to write a function that finds the first word in a string/sentence. Its in relation to a html/css/javascript assignment, where i need to color or mark the first word in a long string, containing a story.
I'm thinking a simple for loop could do it, but cant get it to work.
The String global object is a constructor for strings or a sequence of characters.
in javascript String object has methods on his prototype - MDN - String
String.prototype.split() - Reference (MDN)
The split() method splits a String object into an array of strings by separating the string into substrings, using a specified separator string to determine where to make each split.
because you want to split by words, you should split string by "space".
as you can see split method on string return an array, so the first item in array will be the first word.
'Some String Here'.split(' ')[0];
Good Luck!
// Get Results Element
var div = document.querySelector('.results');
// Some string
var someString = 'Hi Hello JavaScript World';
function renderFirstWorkAsMarked(string, color) {
var splitedString = string.split(' ');
var wrapper = document.createElement('div')
var marked = document.createElement('span');
var restString = document.createTextNode(splitedString.slice(1).join(' '))
marked.style.color = color;
marked.innerHTML = `${splitedString[0]} `;
wrapper.appendChild(marked)
wrapper.appendChild(restString)
return wrapper
}
// append on screen
div.append(renderFirstWorkAsMarked(someString, 'red'))
// This is example code for your full question.
<div class="results"></div>
This will do the trick. It splits the string by the whitespace and then provides you the first word using the index.
"Plane Jane Mane".split(" ")[0]
Here's an example, the first console log will show you the formed array, and the second will select the first word in the array:
var word = "Plane Jane Mane"
console.log(word.split(" "))
console.log(word.split(" ")[0])
I answer your question with ES6 arrow function. see below code:
const firstWord = string => string.split(' ')[0];
Or you can use regex but I prefer the first function:
const firstWord = string => string.match(/^[\w\d]+/gs)[0];
let string = 'This is a sentence';
let word = string.split(' ')[0];
console.log(word);
Firstly, split sentences. Secondly, Split words and take first:
yourtext
.split(/(?<!\w\.\w.)(?<![A-Z][a-z]\.)(?<=\.|\?)\s/g)
.map(w => w.split(/((\b[^\s]+\b)((?<=\.\w).)?)/g)[1])
Example
I've looked all over the web but couldnt find a good answer to this. I need to write a function that finds the first word in a string/sentence. Its in relation to a html/css/javascript assignment, where i need to color or mark the first word in a long string, containing a story.
I'm thinking a simple for loop could do it, but cant get it to work.
Result
I've,I,Its,I'm

Splitting end of text at period creates empty string

Given the following text
var text="unicorns! and rainbows? and, cupcakes.Hello this is splitting by sentences. However, I am not sure.";
I want to split at every period, there is a period at the end of the sentence and it splits it into an empty string as shown.
(4) ["unicorns! and rainbows? and, cupcakes", "Hello this is splitting by sentences", " However, I am not sure", ""]
What is a good way to split at the period using . but accounting for the end of the text?
You can use .filter(Boolean) to strip out any empty strings, like so:
var text="unicorns! and rainbows? and, cupcakes.Hello this is splitting by sentences. However, I am not sure.";
var splitText = text.split(".");
var nonEmpty = splitText.filter(Boolean);
// var condensed = text.split(".").filter(Boolean);
console.log(nonEmpty);
It may seem like a strange way to do it, but it's easy/efficient, and the concept works like this:
var arr = ["foo", "bar", "", "baz", ""];
var nonEmpty = arr.filter(function (str) {
return Boolean(str);
});
This uses the power of coercion to determine whether the string is empty or not. The only value of a string that will coerce to false is, in fact, an empty string "". All other string values will coerce to true. That is why we can use the Boolean constructor to check whether a string is empty or not.
Additionally, if you want to trim the leading/trailing whitespace off of each sentence, you can use the .trim() method, like so:
var text="unicorns! and rainbows? and, cupcakes.Hello this is splitting by sentences. However, I am not sure.";
var nonEmpty = text.split(".").filter(Boolean).map(str => str.trim());
console.log(nonEmpty);
That's how String#split works (and it's kind of logical that it is). There is nothing after the . in the string so it should be an empty string. If you want to get rid of the empty strings in the array you can filter them out using Array#filter (using an arrow function to mke it simple):
var result = text.split(".").filter(s => s); // an empty string is falsy so it will be excluded
Or use String#match with a simple regex in one go like:
var result = text.match(/[^.]+/g); // matches any sequence of character that are not a '.'
Example:
var text="unicorns! and rainbows? and, cupcakes.Hello this is splitting by sentences. However, I am not sure.";
var resultFilter = text.split(".").filter(x => x);
var resultMatch = text.match(/[^.]+/g);
console.log("With filter:", resultFilter);
console.log("With match:", resultMatch);
Adding filter(Boolean) to split is certainly a workaround, but the problem can be handled directly (and flexibly) by giving a regex to split.
For example, you can split on a regex that ignores periods completely or one that preserves all periods (or other punctuation marks):
const text = "unicorns! and rainbows? and, cupcakes.Hello this is splitting by sentences. However, I am not sure.";
// discard periods
console.log(text.match(/[^.]+/g));
// discard periods and leading whitespace
console.log([...text.matchAll(/(.+?)(?:\.\s*)/g)].map(e => e[1]));
// keep periods
console.log(text.match(/(.+?)\./g));
// keep periods periods but trim whitespace
console.log([...text.matchAll(/(.+?\.)\s*/g)].map(e => e[1]));
// discard various sentence-related punctuation
console.log(text.match(/[^.?!]+/g));

charAt first letter of last word in a string

I am trying to select the first letter of the last word in a string: such as the first letter after the last space.
"Hello World"
I need to select "W"
"Hello World I am young"
need to select "y"
using charAt()
thanks
You can split string by empty space, pop last element and ask for it's first letter:
"Hello World".split(" ").pop().charAt(0); // W
Using lastIndexOf function:
var str = "Hello World";
str.charAt(str.lastIndexOf(' ') + 1)
A string is just an array of characters, so you can access it with square brackets. In order to get the first letter of each word in your string, try this:
var myString = "Hello World"
var myWords = myString.split(" ");
myWords.forEach(function(word) {
console.log(word[0]);
});
For just the last word in a string:
var myString = "Hello World"
var myWords = myString.split(" ");
console.log(myWords[myWords.length-1][0]);
If you are just allowed to use charAt do the following:
Iterate through the whole string and always remember the last position you have found an empty space. If you are at the end of the string take the char next to the position you have remembered for the last empty space you have found.
Be aware of the case that your string ends with an empty space. If this could be the case you will probably need two indexes. Also take care if your string does not contain any empty space.

Whats wrong with this regex logic

I am trying to fetch the value after equal sign, its works but i am getting duplicated values , any idea whats wrong here?
// Regex for finding a word after "=" sign
var myregexpNew = /=(\S*)/g;
// Regex for finding a word before "=" sign
var mytype = /(\S*)=/g;
//Setting data from Grid Column
var strNew = "QCById=20";
var matchNew = myregexpNew.exec(strNew);
var newtype = mytype.exec(strNew);
alert(matchNew);
https://jsfiddle.net/6vjjv0hv/
exec returns an array, the first element is the global match, the following ones are the submatches, that's why you get ["=20", "20"] (using console.log here instead of alert would make it clearer what you get).
When looking for submatches and using exec, you're usually interested in the elements starting at index 1.
Regarding the whole parsing, it's obvious there are better solution, like using only one regex with two submatches, but it depends on the real goal.
You can try without using Regex like this:
var val = 'QCById=20';
var myString = val.substr(val.indexOf("=") + 1);
alert(myString);
Presently exec is returning you the matched value.
REGEXP.exec(SOMETHING) returns an array (see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/exec).
The first item in the array is the full match and the rest matches the parenthesized substrings.
You do not get duplicated values, you just get an array of a matched value and the captured text #1.
See RegExp#exec() help:
If the match succeeds, the exec() method returns an array and updates properties of the regular expression object. The returned array has the matched text as the first item, and then one item for each capturing parenthesis that matched containing the text that was captured.
Just use the [1] index to get the captured text only.
var myregexpNew = /=(\S*)/g;
var strNew = "QCById=20";
var matchNew = myregexpNew.exec(strNew);
if (matchNew) {
console.log(matchNew[1]);
}
To get values on both sides of =, you can use /(\S*)=(\S*)/g regex:
var myregexpNew = /(\S*)=(\S*)/g;
var strNew = "QCById=20";
var matchNew = myregexpNew.exec(strNew);
if (matchNew) {
console.log(matchNew[1]);
console.log(matchNew[2]);
}
Also, you may want to add a check to see if the captured values are not undefined/empty since \S* may capture an empty string. OR use /(\S+)=(\S+)/g regex that requires at least one non-whitespace character to appear before and after the = sign.

Split using regex creates 2 empty elements in array

I need to split a string into 2 pieces using regex, so I used the following code:
var str = "This is a test";
var list = str.split(/(test)/);
Required output:
list = ["This is a ", "test"]
Instead of 2 this gives me 3 elements in the array (last one is empty). I understand that regex finds nothing after the 2nd match so it adds an empty (3rd) element. Is there any way that I can modify my code so I get exactly 2 elements thus avoiding the last empty element?
Note: the above code is a simplified version for which we can use other options besides regex but I would have to use regex.
var str = "This is a test";
var list = str.split(/(test)/,2);
list: ["This is a ", "test"]
Perhaps overkill if you can guarantee that you are only expecting an array of length two but given the nature of the question a more robust solution may be to use Array.filter to remove all empty strings from the array - including entries in the middle of the array which would arise from several delimiters appearing next to each other in your input string.
var list = str.split(/(test)/).filter(
function(v){ return v!=null && v!='' }
);
You can try with checking if last element is empty or not:
var last = list.pop();
last.length || list.push(last);
or:
list[list.length-1].length || list.pop();
or even shorter:
list.slice(-1)[0].length || list.pop();
To handle first empty element (test was there as #Kobi suggested) use:
list[0].length || list.shift();
This is giving me the results you want:
var str = "This is a test";
var list = str.split(/(?=test)/g);
(?= is a lookahead, it doesn't capture the word test so that stays in the array after splitting.

Categories