Split String in Half By Word - javascript

I'm in a situation where I'd like to take a string and split it in half, respecting words so that this string here doesn't get split into this str ing here, rather it would be split into this string here.
I figure a starting step would to be to split the string into an array based on spaces, then calculate length based on those pieces, but in my attempts longer strings end up being split up incorrectly.

Look for the first space before and after the middle, and pick the one closest to the middle.
Example:
var s = "This is a long string";
var middle = Math.floor(s.length / 2);
var before = s.lastIndexOf(' ', middle);
var after = s.indexOf(' ', middle + 1);
if (middle - before < after - middle) {
middle = before;
} else {
middle = after;
}
var s1 = s.substr(0, middle);
var s2 = s.substr(middle + 1);
Demo: http://jsfiddle.net/7RNBu/
(This code assumes that there actually are spaces on both sides of the middle. You would also add checks for before and after being -1.)
Edit:
The check that I talked about in the node would be done correctly like this:
if (before == -1 || (after != -1 && middle - before >= after - middle)) {
middle = after;
} else {
middle = before;
}
Here is a fiddle where you can edit the text and see the result immediately: http://jsfiddle.net/Guffa/7RNBu/11/

I wanted to leave this as a comment but do not have enough rep points. The top solution right now fails pretty easily because it does not check for "-1" when using the indexOf method. See this fiddle:
http://jsfiddle.net/7RNBu/7/
var s = "This is a long strinjjjjjjjjjjjjjjjjg";
var middle = Math.floor(s.length / 2);
var before = s.lastIndexOf(' ', middle);
var after = s.indexOf(' ', middle + 1);
if (middle - before < after - middle) {
middle = before;
} else {
middle = after;
}
var s1 = s.substr(0, middle);
var s2 = s.substr(middle + 1);

You might also care about newlines, tabs, as well as spaces, so I would use a regex like this:
var s = "this string here";
var idx = s.length / 2;
while (idx < s.length && s[idx].match(/\s/) == null)
idx++;
var s1 = s.substring(0, idx);
var s2 = s.substring(idx);
document.getElementById("s1").innerText = s1;
document.getElementById("s2").innerText = s2;
See this fiddle: http://jsfiddle.net/nS6Bj/5/

let str = 'qwerty';
let half = Math.floor(str.length / 2);
str = str.slice(0, half) + ' ' + str.slice(half, str.length);
//output
'qwe rty'

I first thought I had an off-by-one error, but I eventually worked through it. Here's a working example.
Now to break down the logic used:
var calculate = function(initialString) {
var halfwayPoint = Math.floor(initialString.length / 2);
var strArray = initialString.split(' ');
// Caluclate halfway point, then break string into words
var wordFlag; // Will be split point
var charCount = 0;
_.each( strArray, function(word, strArrayIndex) {
if (wordFlag) return false;
// If we have the location, exit
// If charCount is before the halfway point
// and the end of word is after halfway point
// Then set the flag
// We add strArrayIndex to the word length to include spaces
if (charCount <= halfwayPoint &&
((charCount + word.length + strArrayIndex) >= halfwayPoint) ) {
wordFlag = strArrayIndex;
return false;
}
// Increase charCount to be length at the end of this word
charCount += (word.length);
});
if (!wordFlag) return null;
// Split the word array by the flag we figured out earlier
var lineOneArray = strArray.slice(0, (wordFlag + 1));
var lineTwoArray = strArray.slice(wordFlag + 1);
// We now join the word arrays into a string, stripping beginning and ending spaces.
var stOne = (lineOneArray.join(' ')).replace(/^\s\s*/, '').replace(/\s\s*$/, '');
var stTwo = (lineTwoArray.join(' ')).replace(/^\s\s*/, '').replace(/\s\s*$/, '');
// Finally return the split strings as an array.
return [stOne, stTwo];
};
If anyone sees holes in my logic, let me know! I'm pretty sure this works in most cases though.
If you'd like the second string to be longer than the first, (ie have the line break before rather than after the middle word), then don't add +1 to wordFlag.

This will split your string based on word count (not character count, so the exact length of each half could be quite different, depending on the placement of long & short words).
var s = "This is a string of filler text";
var pieces = s.split(" "),
firstHalfLength = Math.round(pieces.length/2),
str1 = "",
str2 = "";
for (var i = 0; i < firstHalfLength; i++){
str1 += (i!=0?" ":"") + pieces[i];
}
for (var i = firstHalfLength; i < pieces.length; i++){
str2 += (i!=firstHalfLength?" ":"") + pieces[i];
}
document.write(s);
document.write("<br />"+str1);
document.write("<br />"+str2);
// Output
This is a string of filler text
This is a string
of filler text
http://jsfiddle.net/daCrosby/7RNBu/2/

<h1>
<span>
// for first half start from 0 till middle
{title.substring(0, title.length / 2)}
</span>
<span>
// second half just point the starting point
{title.substring(title.length / 2)}
</span>
</h1>

Related

Limit number of characters per line and wrap while preserving leading white space

My goal is:
Select a block of text (for this example, just a string).
limit number of characters per line.
preserve leading whitespace for each line and reapply it after text is wrapped.
I'm able to limit the number of characters per line correctly, but I'm having trouble with the white space, etc...
Any help would be appreciated
Fiddle
var str = `i am a string that has new lines and whitespace. I need to preserve the leading whitespace and add it back on after the string has been broken up after n characters.
This line has leading whitespace. Tttttt rrrrrr
ttgvgggjjj. Gyjfry bh jkkfrtuj hhdt iihdrtttg.
Here is another line. Hjkkl gggdetu jcfgjbfftt.
This line has no leading whitespace, so i dont need any reapplied. Jjjxsrg bjlkdetyhk llhfftt`;
function addNewlines(str) {
var result = '';
while(str.length > 0) {
result += str.substring(0, 25) + '<br />';
str = str.substring(25);
}
return result;
}
var newStr = addNewlines(str).toString();
document.getElementById("result").innerHTML = newStr;
Should end up looking something like this:
i am a string that has ne
w lines and whitespace. I
need to preserve the lea
ding whitespace and add i
t back on after the strin
g has been broken up afte
r n characters.
This line has leading
whitespace. Tttttt rr
rrrr ttgvgggjjj. Gyjf
ry bh jkkfrtuj hhdt i
ihdrtttg. Here is ano
ther line. Hjkkl gggd
etu jcfgjbfftt.
This line has no leading
whitespace, so i dont n
eed any reapplied. Jjjx
srg bjlkdetyhk llhfftt
Sometimes when dealing with a new algorithm, is much easier to use two or more passes. So you think how it would work in steps instead of everything at once.
I have an implementation working that have 2 passes: first I join the paragraph lines, and in the second pass I perform the actual splitting.
I'm sure there's a better approach, but this works and is fully commented.
I wasn't sure your version of ES so I made it ES5-compatible.
https://jsfiddle.net/2ngtj3aj/
// Divides a string into chunks of specific size
function chunk(str, size) {
var chunks = [];
while(str) {
chunks.push(str.substring(0, size));
str = str.substring(size);
}
return chunks;
}
// Removes all spaces from the left of a string
function trimLeft(str) {
while(str.substr(0,1) == " ") {
str = str.substr(1);
}
return str;
}
// Repeats a character n times
function repeat(c, n) {
return Array(n + 1).join(c);
}
function addNewlines(str) {
var MAX_COLS = 25; // maximum colums on the text
var DEFAULT_LEADING = 3; // default leading to reapply
var MIN_LEADING = 1; // minimum amount of spacing to be considered a paragraph
var CR = "\n";
var result = '';
var leading = 0;
var chunks = [];
var formattedLines = []; // store the intermediary lines
var startLeadingSpaceLine = -1; // where does a paragrph start
var i, l; // counters
var lines = str.split(CR); // input lines
// In the first pass, we join the paragraph lines
for (i = 0; i < lines.length; i++) {
l = lines[i];
// If line is empty, we don't use it
if (l.trim() == "") continue;
if (l.substr(0, MIN_LEADING) == repeat(" ", MIN_LEADING)) {
// If line has leading whitespace, remove the leading space
l = trimLeft(l);
if (startLeadingSpaceLine > -1) {
// If we are already on a paragraph,
// we don't overwrite the flag
} else {
// But if this is the first line of an paragraph,
// We set a flag to allow to join this line with the next one
// if that contains identation as well
startLeadingSpaceLine = i;
}
// If we are on a paragraph, we don't add this line to the array,
// first we need to wait to see if we have more lines in the paragraph
// We also update the line in the array with the whitespace removed
lines[i] = l;
continue;
} else {
// If line doesn't has whitespace, we check if we have just finished
// an paragraph
if (startLeadingSpaceLine > -1) {
// If we do, then we need to add the previous lines to the array
// Note: if we want to leave a space between lines, we need to use
// join(' ') instead of join('')
var paragraphLines = lines.slice(startLeadingSpaceLine, i).join('');
// We add the whitespace we like
paragraphLines = repeat(" ", DEFAULT_LEADING) + paragraphLines;
formattedLines.push(paragraphLines);
}
}
formattedLines.push(l);
}
// Now we parse again the lines, this time we will divide
// the lines into chunks
for (i = 0; i < formattedLines.length; i++) {
l = formattedLines[i];
// Now check against DEFAULT_LEADAING since we have already changed
// the identation
if (l.substr(0, DEFAULT_LEADING) == repeat(" ", DEFAULT_LEADING)) {
// If line has leading whitespace, remove the leading space
// We aded it before just to be able to detect the paragraph.
l = trimLeft(l);
// Divide the line into chunks. We take into account the space
// we have removed, otherwise the paragraph will bleed to the
// right.
l = chunk(l, MAX_COLS - DEFAULT_LEADING);
// We add leading space to all paragraph lines
for(var j = 0; j < l.length; j++) {
l[j] = repeat(" ", DEFAULT_LEADING) + l[j];
}
// Optional: we add blank lines between paragraphs
l = [" "].concat(l).concat([" "]);
} else {
// If we have a simple line, just divide it into chunks
l = chunk(l, MAX_COLS);
}
// Join the lines with newlines and add to the result
l = l.join(CR);
result += l + CR;
}
return result;
}
var process = function() {
var newStr = addNewlines(input.value).toString();
document.getElementById("result").innerHTML = newStr;
}
var input = document.getElementById("input");
input.addEventListener("change", process);
input.addEventListener("keyup", process);
process();
<h3>RESULTS</h3>
<textarea id="input" rows="10" cols="80">i am a string that has new lines and whitespace. I need to preserve the leading whitespace and add it back on after the string has been broken up after n characters.
This line has leading whitespace. Tttttt rrrrrr
ttgvgggjjj. Gyjfry bh jkkfrtuj hhdt iihdrtttg.
Here is another line. Hjkkl gggdetu jcfgjbfftt.
This line has no leading whitespace, so i dont need any reapplied. Jjjxsrg bjlkdetyhk llhfftt</textarea>
<pre id="result"></pre>
General Logic
Your string already contains all whitespaces. If you add console.log(newStr) to your script and look into your console, you'll see that the whitespaces are already there.
You might want to remove all trailing whitespaces (whitespaces before a new line character starts). You can do that by using replace with a regex: var newStr = addNewlines(str).toString().replace(/\s+(?=\n)/g, "");.
Additionally, since all Tab-Characters ("\t") will be recognized as only 1 character but take up way more space than the others, you might want to replace those with 3 or 4 spaces instead. Something like .replace(/\t/g, " ")
Another thing to take into consideration are newlines that are already present before. You'll want to stop counting there and start a new counter after the already present newline.
Displaying inside a Textarea
var str = `i am a string that has new lines and whitespace. I need to preserve the leading whitespace and add it back on after the string has been broken up after n characters.
This line has leading whitespace. Tttttt rrrrrr
ttgvgggjjj. Gyjfry bh jkkfrtuj hhdt iihdrtttg.
Here is another line. Hjkkl gggdetu jcfgjbfftt.
This line has no leading whitespace, so i dont need any reapplied. Jjjxsrg bjlkdetyhk llhfftt`;
function addNewlines(str) {
var result = '';
str = str.replace(/\t/g, " ");
while(str.length > 0) {
nPos = str.indexOf("\n");
len = nPos > 0 && nPos < 25 ? nPos + 1 : 25;
result += str.substring(0, len) + '\n';
str = str.substring(len);
}
return result;
}
var newStr = addNewlines(str).toString().replace(/\s+(?=\n)/g, "");
document.getElementById("result").value = newStr;
<textarea id="result"></textarea>
Displaying in HTML
If you want to display those whitespaces in HTML, then you can make use of the CSS Property white-space: pre.
var str = `i am a string that has new lines and whitespace. I need to preserve the leading whitespace and add it back on after the string has been broken up after n characters.
This line has leading whitespace. Tttttt rrrrrr
ttgvgggjjj. Gyjfry bh jkkfrtuj hhdt iihdrtttg.
Here is another line. Hjkkl gggdetu jcfgjbfftt.
This line has no leading whitespace, so i dont need any reapplied. Jjjxsrg bjlkdetyhk llhfftt`;
function addNewlines(str) {
var result = '';
str = str.replace(/\t/g, " ");
while(str.length > 0) {
nPos = str.indexOf("<br />");
len = nPos > 0 && nPos < 25 ? nPos + 1 : 25;
result += str.substring(0, len) + '\n';
str = str.substring(len);
}
return result;
}
var newStr = addNewlines(str).toString().replace(/\s+(?=\n)/g, "");
console.log(newStr);
document.getElementById("result1").innerHTML = newStr;
document.getElementById("result2").innerHTML = newStr;
document.getElementById("result3").innerHTML = newStr;
document.getElementById("result4").innerHTML = newStr;
document.getElementById("result5").innerHTML = newStr;
div {
font-family: monospace;
}
<h1>normal</h1>
<div id="result1" style="white-space: normal"></div>
<h1>pre</h1>
<div id="result2" style="white-space: pre"></div>
<h1>nowrap</h1>
<div id="result3" style="white-space: nowrap"></div>
<h1>pre-wrap</h1>
<div id="result4" style="white-space: pre-wrap"></div>
<h1>pre-line</h1>
<div id="result5" style="white-space: pre-line"></div>
Also, in your example you're using the tab character to indent your lines. If you wanted those removed as well, then you'd have to remove all occurences of those. You can do that by using another regex and the replace method like this: var newStr = addNewlines(str).toString().replace(/\s+(?=\n)/g, "").replace(/\t/g, "");.

adding a space to every space in a string, then cycling back around until length is met

I have the following while loop as part of my text justify function. The idea is that I have text strings (str) that need to be justified (spaces added to existing spaces in between words) to equal to a given length (len)
The catch is I can only add one space to an existing space at a time before I iterate over to the next space in the string and add another space there. If that's it for all spaces in the string and it's still not at the required length, I cycle back over to the original space (now two spaces) and add another. Then it goes to the next space between words and so on and so on. The idea is that any spaces between words in the string should not have a differential of more than one space (i.e. Lorem---ipsum--dolor--sit, not Lorem----ipsum--dolor-sit)
From my research, I decided that using a substring method off the original string to add that first extra space, then I will increment the index and move to the next space in the string and repeat the add. Here's my code:
var indexOf = str.indexOf(" ", 0);
if ( indexOf > -1 ) {
while ( indexOf > -1 && str.length < len ) {
//using a regexp to find a space before a character
var space = /\s(?=\b)/.exec(str);
str = str.substring(0, indexOf + 1) + " " + str.substring(indexOf + 1);
//go to next space in string
indexOf = str.indexOf(space, indexOf + 2);
if ( indexOf === -1 ) {
//loops back to beginning of string
indexOf = str.indexOf(space, 0);
}
}
}
finalResults.push(str);
This code works most of the time, but I noticed that there are instances where the cycle of spacing is not correct. For example, it generates the following string:
sit----amet,--blandit
when the correct iteration would be
sit---amet,---blandit
Any assistance in making this code properly iterate over every space (to add one space) in the string once, then cycling back around to the beginning of the string to start over until the desired length is achieved would be most appreciated.
I think it's more efficient to compute the number spaces required in the beginning.
var s = "today is a friday";
var totalLength = 40;
var tokens = s.split(/\s+/);
var noSpaceLength = s.replace(/\s+/g,'').length;
var minSpace = Math.floor((totalLength - noSpaceLength)/(tokens.length-1));
var remainder = (totalLength - noSpaceLength) % (tokens.length-1);
var out = tokens[0];
for (var i = 1; i < tokens.length; i++) {
var spaces = (i <= remainder ? minSpace+1 : minSpace);
out += "-".repeat(spaces) + tokens[i];
}
$('#out').text(out);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="out"></div>
This solution
splits the string (s) into words in an array (a)
finds the number of spaces to be added between all words (add)
finds the remainder of spaces to be added between first words (rem)
then sticks the words with add spaces + one if rem is not exhausted
Code
var s = "Caballo sin Nombre"; // assume one space between words
var len = 21; // desired length
var need = len - s.length;
var a = s.split(/ /); // split s
// need>0 and at least two words
if (need > 0 && a.length>1) {
var add = Math.floor(need / (a.length-1)) + 1; // all spaces need that (+existing)
var rem = need % (a.length-1); // remainder
var sp = '';
while (add-- > 0) sp += ' ';
// replace
var i,res = ''; // result
for (i=0 ; i<a.length-1 ; i++) {
res += a[i] + sp;
if (rem-- > 0) res += ' '; // remainder
}
res += a[i];
s = res;
}
console.log("'" + s + "' is " + s.length + " chars long.");
This function adds the spaces using a global replace, carefully limiting the text size.
function expand (txt, colwidth) {
txt = txt.replace (/\s\s+/, ' '); // Ensure no multiple spaces in txt
for (var spaces = ' ', // Spaces to check for
limit = colwidth - txt.length; // number of additional spaces required
limit > 0; // do while limit is positive
spaces += ' ') // add 1 to spaces to search for
txt = txt.replace (RegExp (spaces, 'g'),
function (r) {
// If limit > 0 then add a space else do not.
return limit > 0 && --limit ? r + ' ' : r
});
return txt;
}
for (var w = 21; w--;) console.log (expand ('this is a test.', w));
Shows this on console:
this is a test.
this is a test.
this is a test.
this is a test.
14 this is a test.

Get current sentence by cursor/caret position

I have a string containing multiple sentences. I also have the current cursor/caret position.
I need to be able to extract the current sentence at the given cursor position.
For example, take this string:
This is the first sentence. And this is the second! Finally, this is the third sentence
If the current cursor position is 33 then the cursor is in the second sentence.
In which case, the result returned should be:
And this is the second!
I only need to use the standard sentence definers of .?!
Any help with this would be greatly appreciated.
Although I am expecting regex to be required, if there is a faster alternative using native methods I would be interested in that also.
Here is a way to achieve what you need: use String#split with /[?!.]/g to get an array of sentences and then iterate over the array to sum up the lengths of the sentences found, and if the index is smaller than the count, return the sentence.
function getSentenceByPos(idx, str) {
pos = 0;
array = str.split(/[?!.]/g);
for (var i=0; i<array.length; i++) {
pos += array[i].length + 1;
if (pos >= idx) {
return array[i];
}
}
}// 26 still 1 then `.`. 51 then `!` - 53 is 3rd sentence!
document.write(getSentenceByPos(53, "This is the first sentence. And this is the second! Finally, this is the third sentence"));
I wanted to add an answer that doesn't use regular expression to split up the
string because doing so is quite inefficient and would likely be very slow on
larger chunks of text.
The most efficient way to do it would probably be to use a couple of loops to search, requiring only 2 passes to find the ends of the sentence.
var sentenceFromPos = function (s, pos) {
var len = s.length,
start,
end,
char;
start = pos;
end = pos;
while (start >= 0) {
char = s.charAt(start);
if (char === '.' || char === '?' || char === '!') {
break;
}
start -= 1;
}
while (end < len) {
char = s.charAt(end);
if (char === '.' || char === '?' || char === '!') {
break;
}
end += 1;
}
return s.substring(start + 1, end + 1).trim();
};
var phrase = 'This is the first sentence. And this is the second! Finally, this is the third sentence';
console.log(sentenceFromPos(phrase, 10));
console.log(sentenceFromPos(phrase, 33));
console.log(sentenceFromPos(phrase, 53));
This function will respect cursors over the limits of the phrases (like ! or .)
function getPhrase(string, cursor) {
phrases = string.match(/.*?(!|\.|$)/g)
basecursor = 0;
phrase = phrases[0]
for(ii=0; ii<phrases.length-1; ii++) {
if (basecursor+phrases[ii].length<cursor) {
phrase = phrases[ii+1]
basecursor += phrases[ii].length
}
}
return(phrase)
}
string = "This is the first sentence. And this is the second! Finally, this is the third sentence"
cursor = 0
phrase = getPhrase(string, cursor)
document.write(phrase)

Javascript: Cut string after last specific character

I'm doing some Javascript to cut strings into 140 characters, without breaking words and stuff, but now i want the text so have some sense. so i would like if you find a character (just like ., , :, ;, etc) and if the string is>110 characters and <140 then slice it, so the text has more sense. Here is what i have done:
where texto means text, longitud means length, and arrayDeTextos means ArrayText.
Thank you.
//function to cut strings
function textToCut(texto, longitud){
if(texto.length<longitud) return texto;
else {
var cortado=texto.substring(0,longitud).split(' ');
texto='';
for(key in cortado){
if(key<(cortado.length-1)){
texto+=cortado[key]+' ';
if(texto.length>110 && texto.length<140) {
alert(texto);
}
}
}
}
return texto;
}
function textToCutArray(texto, longitud){
var arrayDeTextos=[];
var i=-1;
do{
i++;
arrayDeTextos.push(textToCut(texto, longitud));
texto=texto.replace(arrayDeTextos[i],'');
}while(arrayDeTextos[i].length!=0)
arrayDeTextos.push(texto);
for(key in arrayDeTextos){
if(arrayDeTextos[key].length==0){
delete arrayDeTextos[key];
}
}
return arrayDeTextos;
}
Break the string into sentences, then check the length of the final string before appending each sentence.
var str = "Test Sentence. Test Sentence";
var arr = str.split(/[.,;:]/) //create an array of sentences delimited by .,;:
var final_str = ''
for (var s in arr) {
if (final_str.length == 0) {
final_str += arr[s];
} else if (final_str.length + s.length < 140) {
final_str += arr[s];
}
}
alert(final_str); // should have as many full sentences as possible less than 140 characters.
I think Martin Konecny's solution doesn't work well because it excludes the delimiter and so removes lots of sense from the text.
This is my solution:
var arrTextChunks = text.split(/([,:\?!.;])/g),
finalText = "",
finalTextLength = 0;
for(var i = 0; i < arrTextChunks.length; i += 2) {
if(finalTextLength + arrTextChunks[i].length + 1 < 140) {
finalText += arrTextChunks[i] + arrTextChunks[i + 1];
finalTextLength += arrTextChunks[i].length;
} else if(finalTextLength > 110) {
break;
}
}
http://jsfiddle.net/Whre/3or7j50q/3/
I'm aware of the fact that the i += 2 part does only make sense for "common" usages of punctuation (a single dot, colon etc.) and nothing like "hi!!!?!?1!1!".
Should be a bit more effective without regex splits.
var truncate = function (str, maxLen, delims) {
str = str.substring(0, maxLen);
return str.substring(0, Math.max.apply(null, delims.map(function (s) {
return str.lastIndexOf(s);
})));
};
Try this regex, you can see how it works here: http://regexper.com/#%5E(%5Cr%5Cn%7C.)%7B1%2C140%7D%5Cb
str.match(/^(\r\n|.){1,140}\b/g).join('')

How can i match and replace a string and its before one letter in javascript?

var str="itss[BACK][BACK][BACK][BACK][BACK][BACK] it's a test stringgg[BACK][BACK]";
var word = '[BACK]';
var substrings = str.split(word);
var cnt= substrings.length - 1;
for(var i = 0;i<cnt;i++){
str = str.replace(/.{1}\[BACK\]{1}/i,""); //remove backspace and one character before it.
}
The above script returns something like "[BACK it's a test string" I need to get this result as "it's a test string" please help me....
It's easier to do this without a regex actually.
String.prototype.replaceFromIndex=function(index, length, replace) {
return this.substr(0, index) + replace + this.substr(index+length);
}
var search = '[BACK]';
var str="itss[BACK][BACK][BACK][BACK][BACK][BACK] it's a test stringgg[BACK][BACK]";
while((index = str.indexOf(search)) >= 0){
str = str.replaceFromIndex(index-1, search.length+1, '');
}
alert(str);
Check http://jsfiddle.net/fRThH/2/ for a working example.
Wrap it in a function and you are ready to go!
Courtesy to Cem Kalyoncu ( https://stackoverflow.com/a/1431113/187018 ) for a slightly modified version of String.prototype.replaceAt
My idea is to count all the backspaces [BACK] and then replace them with an empty string one by one:
var str="itss[BACK][BACK][BACK][BACK][BACK][BACK] it's a test stringgg[BACK][BACK]";
var backspaces = str.match(/\[BACK\]/g).length;
for(i=0; i<backspaces; i++)
{
str = str.replace(/.?\[BACK\]/, '');
}
document.write( str );
working example: jsFiddle
If I understood correctly
var dat = str.split('[BACK]').filter(function(e){return e})[1];
here is the working demo.
One of the problems that I found out was that you didn't set a condition in which you would not have to remove the first character when the string '[BACK]' is in position zero.
Well, the solution I am posting here first search for the position of the first '[BACK]' string, and then creates a substring of the characters that we want to remove, so, if there is a character before the string '[BACK]', it is included in the substring. Then, the substring is removed from the main string, and it continues looping until all the '[BACK]' s are removed.
var str = "itss[BACK][BACK][BACK][BACK][BACK][BACK] it's a test stringgg[BACK][BACK]";
var word = '[BACK]';
var substrings = str.split(word);
var cnt = substrings.length - 1;
for (i = 0; i < cnt; i++) {
pos = str.search("[BACK]");
if (pos - 1 > 0) {
str = str.replace(str.substring(pos - 2, pos + 5), '');
} else {
str = str.replace(str.substring(pos - 1, pos + 5), '');
}
}
Here is the code in jsfiddle:

Categories

Resources