Remove every 'null' in a string

veena_kvkk88
Mega Guru

Hello all!

Hope you are having a great Monday. I have a scripting question, might be simple but I'm not very good with Regular Expressions. Any help is appreciated.

So I have this string field, say 'Summary' and I'm storing the values of 8 other fields appended to one another into this field. There are 4 fields which might have 'none' as an option. So, when I print this 'Summary' and if any of the fields have 'none' as an option or if one of the string fields is left empty, 'Summary' shows 'null' in its place. I don't want this. If it's just one field, I would write an if-else condition to check if null and print accordingly. But since it's 4 fields an any no. of fields can be left empty, there's just too many conditions to consider.

So I came up with this solution. I'm appending every field value into 'Summary' and then check it's contents for the word 'null' and replace every occurrence with a space or something that results in removing it from the string. This is the Regular expression I came up with (not sure if it's right) -> (^|\s)null(?=\s|$)

But I'm not sure how to iterate through the string and remove every occurrence. Please help!!! Thanks in advance.

1 ACCEPTED SOLUTION

veena_kvkk88
Mega Guru

I got it!



var regex = '(^|\s)null(?=\s|$)';


summary = summary.replace(/null/gi, '');



This removed every occurrence of null from the variable summary. The rest is pretty straightforward (setting the value in the field)



Thanks Abhinay for trying to help.


View solution in original post

3 REPLIES 3

Abhinay Erra
Giga Sage

When you say summary, are you talking about email notification?


No Abhinay. It's just a string field in another table. It displays the concatenated string formed by appending multiple other string fields.



All that other functionalities are achieved, so no question about that. I only need to know how to remove all occurrences of 'null' in a string field.


veena_kvkk88
Mega Guru

I got it!



var regex = '(^|\s)null(?=\s|$)';


summary = summary.replace(/null/gi, '');



This removed every occurrence of null from the variable summary. The rest is pretty straightforward (setting the value in the field)



Thanks Abhinay for trying to help.