38
Views
7
Comments
Expression With Styling
Application Type
Mobile, Reactive

Hi

I have expression as mentioned below. I want some part of the expression to be in differnt color and the whole sentence to be in one line

eg: "Your time starts from now : " + CurrDateTime() + " and time ends here."

Only the currdatetime part should be in red color like this

"Your time starts from now : 03-03-2025 01:04:200 and time ends here." 

2026-01-28 16-57-48
Mihai Melencu
Champion

Hello @Touseef Ahmed ,

Unfortunately, expressions don't support direct in-line styling. You'll need to use a separate expression widget for each part that requires different styling. 

For your case:

Expression 1: Your time starts from now :

Expression 2: 03-03-2025 01:04:200 

Expression 3: and time ends here.


Apply the styles you want for each.

2023-10-21 19-42-11
Tousif Khan
Champion

Hello, You can modify the text you have in expression is such a way that your time will be bind in a separate expression and rest of the sentence in other expressions.
you can now apply the desired css to your expression.

Thanks
Tousif Khan

2022-12-30 07-28-09
Navneet Garg
ExpressionDemo.oml
2016-04-22 00-29-45
Nuno Reis
 
MVP

Hello.

CSS:

Pure CSS cannot directly target and style specific words inside a text node without wrapping them in an element.

You can do sequential elements (<span> for instance) and with a class paint some of them. Or even use nth-child to simplify the code visually.

The closest solution to what you are asking, is if that expressions goes into HTML Element widget. But the solutions provided above by others are quite standard.


JS:

This will work if you know the position. For your example, it is word 6 and 7, so positions 5 and 6 of the array:

document.addEventListener("DOMContentLoaded", function () {
    let paragraph = document.getElementById("someText");
    let words = paragraph.innerHTML.split(" "); // Split text into words

    if (words.length > 7) {
        words[5] = `<span class="highlight">${words[5]}</span>`; // Wrap the second word
        words[6] = `<span class="highlight">${words[6]}</span>`; // Wrap the second word
        paragraph.innerHTML = words.join(" "); // Rejoin the text with the span
    }
});


But if you are translating, the number of words may vary. So, we  should go with RegEx:

document.addEventListener("DOMContentLoaded", function () {
    let paragraph = document.getElementById("someText");
    let text = paragraph.innerHTML;

    // Regular expression to match date and time
    let dateTimeRegex = /\b\d{1,2}[-\/]\d{1,2}[-\/]\d{2,4}\s\d{1,2}[:]\d{1,2}[:]\d{1,3}\b/g;
    // Replace the patterns with highlighted span
    paragraph.innerHTML = text.replace(dateTimeRegex, match => `<span class="highlight">${match}</span>`);
});


Notice that for some reason you have 3 digits for seconds so I changed the regex to {1,3} when it should be again [1,2}.

2025-02-07 09-54-44
Sivasakthi Munusamy

Hi @Touseef Ahmed ,

can you pls refer the below attached oml, If it helps.

time.oml
2023-11-22 10-51-50
Jozy Sohail

hi Touseef,

you can use the label and inside it you can use different text or expression and design them as per your needs by giving the style classes.

hope it helps.

2026-02-16 05-51-10
Sahana K

Hi @Touseef Ahmed ,

Thanks,
Sahana

Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.