Replicate string in a certain part of a text (javascript/Jquery)

Asked

Viewed 64 times

0

I have a text and would like to replicate strings in a certain part of this using javascript or Jquery

Text = Jn 3:16; 7:1,3; 3:20; Mt 2:3,5,2-7; 3:1; Lk 2:1; 3:12
What I’ve got so far: Jo 3:16; Jo 7:1,3; 3:20; Mt 2:3,5,2-7; Mt 3:1; Lc 2:1; Lc 3:12
Desired result: Jo 3:16; Jo 7:1.3; Jo 3.20; Mt 2:3,5,2-7; Mt 3:1; Lc 2:1; Lc 3:12

Below is what I have achieved so far, I can only add one more sequence

$(document).ready(function(){

  var str = document.getElementById("resultado").innerHTML; 
  var res = str
.replace(/(Jo|Mt|Lc) (([0-9]{1,3}(\.|:)[0-9]{1,3}((,| ,|, |-| e )[0-9]{1,3}){1,})|([0-9]{1,3}(\.|:)[0-9]{1,3}))(;|)( [0-9]{1,3}(\.|:))/ig, "$1 $2; $1$10")
.replace(/(Jo|Mt|Lc) (([0-9]{1,3}(\.|:)[0-9]{1,3}((,| ,|, |-| e )[0-9]{1,3}){1,})|([0-9]{1,3}(\.|:)[0-9]{1,3}))(;|)( [0-9]{1,3}(\.|:))/ig, "$1 $2; $1$10")
document.getElementById("resultado").innerHTML = res;
//r = document.getElementById("resultado").innerHTML = res;
console.log(r)

});
<html>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class="resultado" id="resultado">
Jo 3:16; 7.1,3; 3:20; Mt 2:3,5,2-7; 3:1; Lc 2:1; 3:12
</div>

</html>

  • You can put the string inside a variable, and in the text you replicate this variable as many times as you want by declaring it in the desired snippets.

  • I can already put in a variable, what I need now is to get the strings distributed correctly in the text

1 answer

0


Follows below as I solved, may not be the ideal code but served

$(document).ready(function(){

  var str = document.getElementById("resultado").innerHTML; 
  var res = str
.replace(/(Jo|Mt|Lc) (([0-9]{1,3}(\.|:)[0-9]{1,3}((,| ,|, |-| e )[0-9]{1,3}){1,})|([0-9]{1,3}(\.|:)[0-9]{1,3}))(;|)( [0-9]{1,3}(\.|:))/ig, "$1 $2; $1$10")
.replace(/(Jo|Mt|Lc) (([0-9]{1,3}(\.|:)[0-9]{1,3}((,| ,|, |-| e )[0-9]{1,3}){1,})|([0-9]{1,3}(\.|:)[0-9]{1,3}))(;|)( [0-9]{1,3}(\.|:))/ig, "$1 $2; $1$10")
.replace(/(Jo|Mt|Lc) (([0-9]{1,3}(\.|:)[0-9]{1,3}((,| ,|, |-| e )[0-9]{1,3}){1,})|([0-9]{1,3}(\.|:)[0-9]{1,3}))(;|)( [0-9]{1,3}(\.|:))/ig, "$1 $2; $1$10")
.replace(/(Jo|Mt|Lc) (([0-9]{1,3}(\.|:)[0-9]{1,3}((,| ,|, |-| e )[0-9]{1,3}){1,})|([0-9]{1,3}(\.|:)[0-9]{1,3}))(;|)( [0-9]{1,3}(\.|:))/ig, "$1 $2; $1$10")
.replace(/(Jo|Mt|Lc) (([0-9]{1,3}(\.|:)[0-9]{1,3}((,| ,|, |-| e )[0-9]{1,3}){1,})|([0-9]{1,3}(\.|:)[0-9]{1,3}))(;|)( [0-9]{1,3}(\.|:))/ig, "$1 $2; $1$10")
document.getElementById("resultado").innerHTML = res;
//r = document.getElementById("resultado").innerHTML = res; console.log(r);

});
<html>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class="resultado" id="resultado">
Jo 3:16; 7.1,3; 3:20; Mt 2:3,5,2-7; 3:1; Lc 2:1; 3:12
</div>

</html>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.