How to trim the string in javascript?

Experts,
 We have some java script and reading data from sharepoint list. Some list item has more space and we need to trim it before we compare. The  original value is "California          " and we need to replace to "California".

Can you please help me write some code?

original code
  var spClientContext = SP.ClientContext.get_current();
        var parentListName = 'DM Nomination List'; 
		var spList = spClientContext.get_web().get_lists().getByTitle(parentListName);
		var objResult;
		    
		    arrLstDMNomination = [];
		    
			var viewXml = "<View><Query><Where><Eq><FieldRef Name='Nominated' /><Value Type='Text'>1</Value></Eq></Where>";
				viewXml += "<GroupBy Collapse=\"TRUE\" ><FieldRef Name=\"Territory\"/></GroupBy></Query><ViewFields><FieldRef Name=\"LinkTitle\"/>";
				viewXml += "<FieldRef Name=\"Title\"/><FieldRef Name=\"Territory\"/></ViewFields><Aggregations Value=\"On\">";
				viewXml += "<FieldRef Name=\"Nominated\" Type=\"COUNT\"/></Aggregations></View>";
			 var groupBy = spList.renderListData(viewXml);
			 spClientContext.executeQueryAsync(function(){
			 objResults = JSON.parse(groupBy.m_value);
 			 
			  $.each(objResults.Row, function(idx, obj){
                var vTerritoryName = obj["Territory"];
			 	var vNominatedCnt = obj["Territory.COUNT.group"];
			 	arrLstDMNomination[idx] = { 'TerritoryName' : vTerritoryName, 'NominatedCount' : vNominatedCnt  };
              });	 
            //Step 4  
			//fnReadDMPCSelectedData();	          
 
			},function(e){
			 console.log(e);
			})

Open in new window

sharepoint0520Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

leakim971MultitechnicianCommented:
var str = "California&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
str = str.replace(/^(&nbsp;)*|(&nbsp;)*$/g,"");

Open in new window

Capture-d-e-cran-2019-03-04-a--18.40.png
sharepoint0520Author Commented:
Thank you. It's object so where should i implement in attached code?
leakim971MultitechnicianCommented:
in your comparaison code, I don't see it...
Amazon Web Services

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

sharepoint0520Author Commented:
Hi,
 I am using this code for comparision. I tried to add your code but did not work out.
$.each(arrLstDMNomination, function(idxC, objC){
			        	    objC.TerritoryName.replace(/^(&nbsp;)*|(&nbsp;)*$/g,"");	
			        	    
			               if(objC.TerritoryName.replace("&amp;","&") == objA.TerritoryName){
			                  vDMNomCnt = objC.NominatedCount;
			                  vDMNomination = parseInt(vDMNomination) + parseInt(vDMNomCnt);
			                  }
			        	});

Open in new window

sharepoint0520Author Commented:
Hi, One more thing. There is extra space at end and that is why it's not working.

var str = "California&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ";
str = str.replace(/^(&nbsp;)*|(&nbsp;)*$/g,"");

And i have changed the code to compare

if(objC.TerritoryName.replace("&amp;","&") == objA.TerritoryName)
			                  {
			                  vDMNomCnt = objC.NominatedCount;
			                  vDMNomination = parseInt(vDMNomination) + parseInt(vDMNomCnt);
			                  }
			                  
			                  if(objC.TerritoryName.replace(/^(&nbsp;)*|(&nbsp;)*$/g,"") == objA.TerritoryName)
			                  {
			                  vDMNomCnt = objC.NominatedCount;
			                  vDMNomination = parseInt(vDMNomination) + parseInt(vDMNomCnt);
			                  }

Open in new window

But not working.
leakim971MultitechnicianCommented:
so replace :
objC.TerritoryName.replace("&amp;","&")
by :
objC.TerritoryName.replace(/^(&nbsp;)*|(&nbsp;)*$/g,"")
leakim971MultitechnicianCommented:
if doesn't work, try this too:
if(objC.TerritoryName.replace(/^(&nbsp;)*|(&nbsp;)*$/g,"") == objA..replace(/^(&nbsp;)*|(&nbsp;)*$/g,""))
sharepoint0520Author Commented:
Thank you Sir.  There was a space after last" &nbsp;" so i had to replace " " to ""  again to accomplished.

var str = "California&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ";
str = str.replace(/^(&nbsp;)*|(&nbsp;)*$/g,"");

Thank you Sir.
leakim971MultitechnicianCommented:
remove also leading and ending space :
.replace(/^(&nbsp;)*|(&nbsp;)*$|^\s|\s$/g,"") 

Open in new window

sharepoint0520Author Commented:
Hi,
 Can you please check this on Browser?  It's not working. If i remove space from end then it's going to work out.

var str = "California&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ";
str = str.replace(/^(&nbsp;)*|(&nbsp;)*$|^\s|\s$/g,"");

SC
Thank You
leakim971MultitechnicianCommented:
\s*
Instead
\s
sharepoint0520Author Commented:
Hi,
 Sorry to say but it did not work. Did you get chance to run on Browser?  I came up with alternate option but i have to replace twice.

var str = "California&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ";
str = str.replace(/&nbsp;/gi,"").replace(" ","");

But this is not a ideal solution because if string has more  spaces at end then it will not work.
leakim971MultitechnicianCommented:
I'm on mobile phone, did you ads wildcard After each \s ?
\s*
sharepoint0520Author Commented:
I did it.  And this code also remove the space between two words too (that i did not expect). Please validate once you get chance.

Thank you so much for helping me.
leakim971MultitechnicianCommented:
var str = "California&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ";
str = str.replace(/^(&nbsp;\s?)*|(&nbsp;\s?)*$/g,""); 

Open in new window

leakim971MultitechnicianCommented:
var str = "California&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ";
str = str.replace(/^(&nbsp;\s*?)*|(&nbsp;\s*?)*$/g,""); 

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sharepoint0520Author Commented:
Awesome. It's working like charm. Thanks a lot.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.