0

I have a Array of Strings. Its a text with enumeration. So its like "1. abcde. 2. fghijk. There is no new line. I want to write a loop that detects the place where the "2." is and the place before it (array[I-1]) there should be included a \n for the new line. But I don't know a command for place a new element in an array without deleting the existing element on the same place. How can I do that ?

This is the code I got till now:

let el = document.getElementById('textinhalt').textContent;

for(let i = 0; i<el.length; i++){
if(el[i]=="2" && el[i+1]=="."){
 
 }
}
kaan5353
  • 67
  • 5
  • please share some code. – Daniel A. White Jul 16 '21 at 12:47
  • You call it an array in your post, but it looks like a string, which you're treating as an array of characters in your code. I think you'd find this much more convenient if you can just treat your data as an array of strings as `const data = ['abcde', 'fghijk', 'etc']`. It would be much easier to insert/delete/whatever into that structure than into a single string. – Chris Farmer Jul 16 '21 at 12:51
  • To push (add to the end) to an array, use [`push()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push), to add/modify "in between", use [`splice()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice) – ΔO 'delta zero' Jul 16 '21 at 12:52
  • Does this answer your question? [How to append something to an array?](https://stackoverflow.com/questions/351409/how-to-append-something-to-an-array) – Heretic Monkey Jul 16 '21 at 13:05

2 Answers2

1

You don't need loop. You can simply use replace or replaceAll

both takes regex as needle, so you can check for another digits like 2., 3., 4., 5. and so on with /([2-9]\.)/ - this regex will find all signle digits with . after it, that is not 0 or 1

try this regex at regex101 and see how it works

let el = document.getElementById('textinhalt').textContent;
document.getElementById('textinhalt').innerHTML = el.replace(/([2-9]\.)/, '<br>$1');
<div id="textinhalt">1. abcde. 2. fghijk</div>

UPDATE

for bigger number I would suggest another regex
/(?:[^1](\d+\.))/g and you can test it again on regex101

demo:

let el = document.getElementById('textinhalt').textContent;
document.getElementById('textinhalt').innerHTML = el.replaceAll(/(?:[^1](\d+\.))/g, '<br>$1');
<div id="textinhalt">1. abcde. 2. fghijk 15. asdasd 10000. dsfsdfsdfsd</div>
ciekals11
  • 2,032
  • 1
  • 10
  • 24
  • 1
    I would even support larger numbers with `/([2-9]|([1-9][0-9]+)\.)/` –  Jul 16 '21 at 13:07
  • @jabaa I have updated my answer with regex that will support up to infinite numbers. It is a bit overcomplicated because of this first element but it works. – ciekals11 Jul 16 '21 at 13:23
0

You would have to parse the text, insert the line before the index, and serialize it.

const parse = (text) => text.split(/\s*\d+\.\s/).filter(l => l.length > 0);
const serialize = (arr) => arr.map((l, i) => `${i + 1}. ${l}`).join(' ');
const insertBefore = (arr, i, val) => arr.splice(i, 0, val);

const text = '1. abcde. 2. fghijk.';
const arr = parse(text);

insertBefore(arr, 1, 'Hello World.');

arr.push("I'm last!");

console.log(serialize(arr));
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132