AurumPotabile
AurumPotabile

Reputation: 431

Remove extra ;# instances from string

This is a JavaScript question.

I have a string (a list of SharePoint account names) that can have a user account dropped out at any point in the string. Example:

"1;#Smith, John;#47;#Doe, Jane;#13;#Bronte, Charlotte"

I have my code set up to drop out one of the account strings based on user selection, but that leaves a ;# separator either at the beginning, in the middle, or at the end of the string.

Dropping John Smith:

";#47;#Doe, Jane;#13;#Bronte, Charlotte"

Dropping Jane Doe:

"1;#Smith, John;#;#13;#Bronte, Charlotte"

Dropping Charlotte Bronte:

"1;#Smith, John;#47;#Doe, Jane;#13;#"

Can you provide a regex I can use to kill the remaining offending ;#?


Here's the removal code, where existingUsers is the full string, and account is the name to drop from the string:

if (existingUsers.length > account.length) {
  existingUsers.replace(account, "");
  // Clean up leftover ;# - regex
} else {
  existingUsers = "";
}

Upvotes: 0

Views: 122

Answers (2)

Vadim Gremyachev
Vadim Gremyachev

Reputation: 59328

The following expression could be used for parsing SharePoint user field value:

((\d+));[#]([(\w*\\)+\s]+)

How to parse user field value in JavaScript

function parseMultiColumnValue(fieldValue)
{
    var re = /((\d+));[#]([(\w*\\)+\s]+)/g;
    var results = [],match;
    while (match = re.exec(fieldValue)) {
       results.push({'Id' : parseInt(match[1]), 'Value': match[3]});
    }
    return results;
}  



//Tests
   
//Parse values
var values = parseMultiColumnValue('1;#user1;#4;#user2;#10;#user3');
$('div#output').append(JSON.stringify(values));

//Delete the first item 
values.splice(0, 1);
$('div#output').append(JSON.stringify(values));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="output"/>

Upvotes: 1

Matt Burland
Matt Burland

Reputation: 45135

var r = /(^;#|;#(?=;#)|;#$)/;
var s1 = ";#47;#Doe, Jane;#13;#Bronte, Charlotte";
var s2 = "1;#Smith, John;#;#13;#Bronte, Charlotte";
var s3 = "1;#Smith, John;#47;#Doe, Jane;#13;#";

console.log(s1.replace(r,""));
console.log(s2.replace(r,""));
console.log(s3.replace(r,""));
<script src="https://getfirebug.com/firebug-lite-debug.js"></script>

This simply matched either:

^;#         start of string followed by ;#
;#(?=;#)    ;# followed by another ;#
;#$         ;# followed by end of string

As others have suggested, it might be easier to just split the string into an array. You can join it back together again if you must have a string back:

var s = "1;#Smith, John;#47;#Doe, Jane;#13;#Bronte, Charlotte";
var account = "1;#Smith, John";   // for example
var search = account.split(";#");
var split = s.split(";#");    
var i = split.indexOf(search[0]);  // search by number first
if (i!==-1 && split[i+1] === search[1]) {   // check the username matches too!
    split.splice(i,2);         // we remove the matching elements
    console.log(split);        // John Smith was removed
}
console.log(split.join(";#")); // If you must have a string after removing a user
<script src="https://getfirebug.com/firebug-lite-debug.js"></script>

Upvotes: 0

Related Questions