I have a simple code, with which I propose to perform an operation and when it works, place it in a larger code, I need to place in an input what is before Price: and After " - " in real time.
For example in a text string that contains "SOFT DRINK 2L HIT-ORANGE - COCACOLA Price: 1500.00"
I want the word COCACOLA , since it is the brand of the product, to be saved in a variable to print it in an input, but it is about saving what is after " - " and before "Price:"
For example, with the following code, I can obtain what is before Price:
const PrincAct = /\w+(?= Precio)/;
const palabra = m1.match(PrincAct)
document.getElementById(prinact).value = palabra;
The problem lies in the fact that the following "SOFT DRINK 2L NARANJA-ORANGE Price: 1000.00" can exist in a text string. The word ORANGE was saved in my input and I do not want this to happen, because ORANGE is not a registered trademark .
As additional data in my text string there are 2 equal signs "-" the difference is that one is separated by spaces. and this one separated by space is my reference to take to capture the word that is before Price
Another fact is that this code multiplies and totals the price value.
I want to achieve it using only JavaScript
<!DOCTYPE html>
<html lang="es_ES">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script type="text/javascript">
var nextinput = 0;
function AgregarCampos() {
nextinput++;
campo = '<li id="idLi' + nextinput + '">' +
'<select id="txt' + nextinput + '" step="any" oninput="multiplicar(\'txt' + nextinput + '\',\'nmb' + nextinput + '\',\'acumu' + nextinput + '\',\'prinact' + nextinput + '\');" >' +
'<option value="REFRESCO 2L HIT-NARANJA - COCACOLA Precio: 1500.00">REFRESCO 2L HIT-NARANJA - COCACOLA Precio: 1500.00</option> ' +
'<option value="REFRESCO 2L GOLDEN-NARANJA - PEPSICOLA Precio: 1200.00">REFRESCO 2L GOLDEN-NARANJA - PEPSICOLA Precio: 1200.00</option> ' +
'<option value="REFRESCO 2L NARANJA-ORANGE Precio: 1000.00">REFRESCO 2L NARANJA-ORANGE Precio: 1000.00"</option> ' +
'</select>' +
'<input type="text" size="15" id="prinact' + nextinput + '" step="any" oninput="multiplicar(\'txt' + nextinput + '\',\'nmb' + nextinput + '\',\'acumu' + nextinput + '\',\'prinact' + nextinput + '\');" />' +
'<input type="number" size="15" id="nmb' + nextinput + '" step="any" oninput="multiplicar(\'txt' + nextinput + '\',\'nmb' + nextinput + '\',\'acumu' + nextinput + '\',\'prinact' + nextinput + '\');" />' +
'<input type="text" size="15" id="acumu' + nextinput + '" step="any" class="monto"/></li>';
$("#campos").append(campo);
}
function multiplicar(txt, nmb, acumu, prinact) {
var m1 = document.getElementById(txt).value;
var m2 = document.getElementById(nmb).value;
const PrincAct = /\w+(?= Precio)/;
const palabra = m1.match(PrincAct)
document.getElementById(prinact).value = palabra;
var P = /precio: (\d+)/i;
var matchA = m1.match(P);
var Price = parseInt(matchA[1]);
r = Price * m2;
document.getElementById(acumu).value = r;
var total = 0;
$(".monto").each(function () {
if (isNaN(parseFloat($(this).val()))) {
total += 0;
} else {
total += parseFloat($(this).val());
}
});
document.getElementById('T').value = total;
}
</script>
</head>
<body>
<form id="form" name="form" method="post">
<div align="left">
<a href="#" onclick="AgregarCampos();">Agregar Producto</a>
<ul id="campos"></ul>
<tr>
<td>
Total
</td>
<td>
<input type="text" id="T" value="0" disabled>
</td>
</tr>
</div>
</form>
</body>
</html>
I appreciate the comments you can give me to achieve what I need?
Taking into account the structure of your string, we can use a regular expression to obtain the price
Putting a period and more numbers after it as optional content, since solo
[0-9]{1,}$
won't match100.00
After that, we are going to separate the string with
String.prototype.split
to separate the stringObviously, if the first element of the array that it gives us is equal to the complete string, there is no mark
If it is not equal to the complete string, we get the second element of the array which will contain a template like this
So, we use
String.prototype.trim
to remove unnecessary spaces at the beginning and end of the second element of the array, then we useString.prototype.split
again to separate the element by each space, we will have something like thisAnd obviously the first element of the new array will be the mark
To stop me from marking an error on this line
Just remove the brackets and the variable r leaving it like this:
This way I had to add my variables again to multiply
In the processStr(s) function I had to accommodate the line that you placed where it returns because if I didn't do it where I want COCACOLA to be recorded , the price would also be displayed, being like this COCACOLA,1500.00
To this Line, I removed the variable p, so that it would not bring me the numerical value of price
Being that way:
This way it no longer gives me an error and does what I need.
Thank you very much anything