The following function cuts a string, you only have to enter the text, the beginning and the end.
The function is as follows:
Generator.prototype.splitbywords = function (variable, start, length) {
var s = variable,
len = s.length,
posStart = Math.max(0, start == 0 ? 0 : s.indexOf(' ', start)),
posEnd = Math.max(0, length > len ? len : s.indexOf(' ', length));
return s.substr(posStart, posEnd);
};
The function works well almost always , but it does not cover some cases and I would like your help.
These are the cases:
- If the last space does not exist
for the following text:
"PayPal, your entrance to the international market" (string of 44 characters)
It is not returning anything, for the function:
splitbywords("PayPal, tu entrada al mercado internacional",0,40)
And the reason is that it doesn't find the last space after the long 40
s.indexOf(' ', length)
- Don't return the comma ","
I also want it to not break the text after a comma, and the problem is on the same line:
posEnd = Math.max(0, length > len ? len : s.indexOf(' ', length));
since I am querying for a space " "
and not a comma ", "
. I tried to fix this by changing this line to:
posEnd = Math.max(0, length >= len ? len : (s.charAt(s.substr(posStart, s.indexOf(' ', length)) - 1) = "," ? s.indexOf(', ', length): s.indexOf(' ', length));
But the comma keeps returning and I don't understand what I did wrong.
I have added some test:
string = "Hola amigos ¿como están?"
splitbywords(string, 0, 10)
Retorna: "Hola amigos"
CORRECTO
splitbywords(string, 0, 20)
Retorna: ""
PROBLEMA ya que espero que retorne todo el texto
=======================
string = "Hola Julio, estoy bien"
splitbywords(string, 0, 10)
Retorna: "Hola Julio,"
PROBLEMA, no quiero que retorne la coma ","
splitbywords(string, 0, 15)
Retorna: "Hola Julio, estoy"
CORRECTO
======================
EDIT2: The code looks like this:
function splitbywords(variable, empieza, termina) {
var s = variable+' ', // resuelve problema 1
largo = s.length,
posStart = Math.max(0, empieza == 0 ? 0 : s.indexOf(' ', empieza)),
posEnd = Math.max(0, termina > largo ? largo : s.indexOf(' ', termina)),
cadena = s.substr(posStart, posEnd);
if (cadena.charAt(cadena.length-1) === ',') { //resuelve problema 2
cadena = cadena.substr(0, cadena.length-1);
}
return cadena;
}
function splitbywords(variable, empieza, termina) {
var s = variable+' ',
largo = s.length,
posStart = Math.max(0, empieza == 0 ? 0 : s.indexOf(' ', empieza)),
posEnd = Math.max(0, termina > largo ? largo : s.indexOf(' ', termina)),
cadena = s.substr(posStart, posEnd);
if (cadena.charAt(cadena.length-1) === ',') {
cadena = cadena.substr(0, cadena.length-1);
}
return cadena;
}
let text1 = "PayPal, tu entrada al mercado internacional";
let text2 = "Hola Julio, estoy bien";
document.getElementById("demo1").innerHTML = splitbywords(text1, 0 , 25);
document.getElementById("demo2").innerHTML = splitbywords(text1, 0 , 30);
document.getElementById("demo3").innerHTML = splitbywords(text2, 0 , 8);
document.getElementById("demo4").innerHTML = splitbywords(text2, 0 , 15);
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript splitbywords</h1>
<p>Ejemplos:</p>
<p id="demo1"></p>
<p id="demo2"></p>
<p id="demo3"></p>
<p id="demo4"></p>
</body>
</html>
You could use
search
to do a search usingRegExp
.As the method
search
cannot be indicated aoffset
(from where to start searching, we use.substr(offset)
, to cut thestring
and start the search from that point.Since it
text.substr(offset).search(re)
returns the position of the character in the trimmed string, it is necessary to add theoffset
to correct the returned position.For example:
The same thing you want to do but with a regular expression:
First, I think it would be more practical for this case to use
substring
instead ofsubstr
.I see that you do quite unnecessary calculations. For example, in the problem where the index to the end exceeds the size of the text, by default, the default function
substring
returns from the start, to the end of the text. The comma problem can be solved just by doing a conditional:We decrement a unit because the method
substring
returns fromX
toN - 1
.You can also play with regular expressions to detect or replace certain characters like commas, spaces, etc. It is up to your creativity.