H
H
heisenberg12021-06-20 17:25:49
JavaScript
heisenberg1, 2021-06-20 17:25:49

How to replace time in template string?

Good day.
Tell me how to get the result "We drove in at 13:00, left at 15:00" (replace the time in the line)?
Replace "10:00" with time.checkin (13:00 for example) and "19:00" with time.checkout (for example 15:00)
The part of the template where you want to replace the time:

<p class="popup__text popup_time">Заехали в 10:00, выехали в 19:00</p>


An example of replacing +- like this:
Displayed the advert.price price in the .popup_price block.
Sample:
<p class="popup__text popup_price">5200 <span>₽/ночь</span></p>

advert.price is a random imported number (eg 3100).
Decision:
const price = advertElement.querySelector('.popup_price');
  price.textContent = price.textContent.replace('5200', advert.price).

I got the result "3100 ₽/night".

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
mikeyuri, 2021-06-20
@mikeyuri

For example:

const checkIn = "13:00";
const checkOut = "15:00";
const str = "Заехали в 10:00, выехали в 19:00";
const re = /(.*?)\d+:\d+(.*?)\d+:\d+/;
const repl = `$1${checkIn}$2${checkOut}`;
const result = str.replace(re, repl);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question