I need a code snippet to parse a text field...

I have a text field in a database that is being displayed in a repeater…

The fields contains:
a] AC-Delco 17D38 (GM 18028648). [b] AC-Delco 17D38M (GM 18028649). [c] Bendix MKD38. [d] Bendix PMD38. [e] NAPA/TruStop TS778. [f] Raybestos PGD38M. [g] Raybestos PGD38R. [h] Wagner MX38

I would like to parse the field and split it on the left bracket so that it appears as:

[a] AC-Delco 17D38 (GM 18028648). [
b] AC-Delco 17D38M (GM 18028649).
[c] Bendix MKD38.
[d] Bendix PMD38.
[e] NAPA/TruStop TS778.
[f] Raybestos PGD38M.
[g] Raybestos PGD38R.
[h] Wagner MX38

Was considering “split” but not quite sure how to do it…
Note: some records might contain only a, b or none at all

Thanks
JD

You can use split (if the only place you use the bracket is the begging of a row):

let text = “[a] AC-Delco 17D38 (GM 18028648). [b] AC-Delco 17D38M (GM 18028649). [c] Bendix MKD38. [d] Bendix PMD38. [e] NAPA/TruStop TS778. [f] Raybestos PGD38M. [g] Raybestos PGD38R. [h] Wagner MX38”;
let array = text.split(“[”);
let string = “”;
let i;
for (i = 1; i < array.length; i++) {
string += “[” + array[i] + “\n”;
}
$w(“#text1”).text = string;

P.S.
But a shorter way to do it is without split(). just:
$w(“#text1”).text = string.replace(/(?=[)/g, “\n”);

Where string stands for the relevant field from the database.
and if you use it in a repeater, you should, of course, use it in onItemReady().

Cant get either method to work??? Here is my code…

import wixData from ‘wix-data’;
import wixWindow from ‘wix-window’;
import wixLocation from ‘wix-location’;

$w.onReady( function () {
$w(“#dynamicDataset”).setFilter(wixData.filter()
.gt(“oe_part_no”, ’ ‘)
)
.then(() => {
$w(“#repeater1”).forEachItem( ($w, itemData, index) => {
let text = $w(’#test94"');
let array = text.split(“[”);
let string = “”;
let i;
for (i = 1; i < array.length; i++) {
string += “[” + array[i] + “\n”;
}
$w(“#text94”).text = string;
} )
.then(() => {
$w(“#repeater1”).show();
})
})

});

export function repeater1_itemReady($w, itemData, index) {
let string = $w(“#text94”).text
$w(“#text94”).text = string.replace(/(?=[)/g, “\n”);
$w(“#repeater1”).show();
}

The problem is not with the parsing methods. They both return the desired result.

let string = “[a] AC-Delco 17D38 (GM 18028648). [b] AC-Delco 17D38M (GM 18028649). [c] Bendix MKD38. [d] Bendix PMD38. [e] NAPA/TruStop TS778. [f] Raybestos PGD38M. [g] Raybestos PGD38R. [h] Wagner MX38”
console.log(string.replace(/(?=[)/g, “\n”));

There must be an issue with displaying it in the repeater.

You need to make sure you are using correct scope arguments. This code should be changed to use $item or similar. $w will cause confusion with global wix element scope and lead to unexpected results.

itemReady($w, itemData, index) {

Try something like this instead:

export function repeater1_itemReady($item, itemData, index) {
  let string = $item("#text94").text
  $item("#text94").text = string.replace(/(?=\[)/g, "\n");
}  

@stevendc I still cannot get this to work!

Here is my code:

import wixData from ‘wix-data’;
import wixWindow from ‘wix-window’;
import wixLocation from ‘wix-location’;

let origField;
let newField;

$w.onReady( function () {
$w(“#origNums”).hide();
});

export function repeater1_itemReady(item, itemData, index) {
$w(“#origNums”).hide();
origField = $w(“#origNums”).text;
newField = origField.replace(/(?=[)/g, “\n”);
$w(“#displayNums”).text = newField;
console.log("from DB: " + origField);
console.log("split: " + newField);
}

HERE IS THE RESULT:

I could not get any of the suggestions to work. I have passed it to my wix consultant…

@cwvega76 in your repeater1_itemReady( ) you declared “item”, so keep using it and not $w.

Even though the program runs and displays the field as it originally was… If I hit F12 the developer console shows error:


I HAVE NO IDEA WHAT THIS MEANS?