How to retrieve text from string in javascript?

Experts,
  We are reading current user automatically by java script. The value store as a "i:0#.w|domain\username". I need to separate domain and username in another variable by javascript.

Can you please help me to write some script to retrieve username and domain?

 
 var input = "i:0#.w|domain\username"l
 var user = "" // Need to Populate
 var domain = "" // Need to Populate

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.

Ryan ChongSoftware Team LeadCommented:
<script>

var input = "i:0#.w|domain\\username";

var tmp = input.split("|");
var tmp2 = tmp[1].split("\\");
var user = tmp2[1];
var domain = tmp2[0];

document.write("user = " + user + "<br>");
document.write("domain = " + domain + "<br>");
 
</script>

Open in new window

leakim971PluritechnicianCommented:
var input = "i:0#.w|domain\username"l
 var x = input.match(/[^|]*\|([^\\]*)\\(.*)/)
 var user = x[2];
 var domain = x[1];

 Capture-d-e-cran-2019-02-27-a--20.59.png

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:
Hi Leakim
 Thank you . It's working now. Actually i need to pass those variables to rest api and based on result i have to write some html. Can you please help me?

Rest API code
Function call()
{
setData="{'migratedSecurityGroup':'Group1','User':'','domain':''}";
    $.ajax({
        type: "POST",
        url: _spPageContextInfo.webAbsoluteUrl+"/_layouts/15/FindUserSecGroupMembership/CheckUserADMembership.aspx/IsMemberOf",
       data:setData,
        async: false,
        contentType: 'application/json; charset=utf-8',
        success: function(data) {
                                                //debugger;
               //if(data.d.length>0){
                console.log(data)
              alert(data);
           //}
                },
        error: function (error) {debugger;
            console.log(JSON.stringify(error));
        }
    });
}

Open in new window


Original Code
<script type="text/javascript" src="_layouts/15/sp.runtime.js"></script>
<script type="text/javascript" src="_layouts/15/sp.js"></script>
<script src="https://code.jquery.com/jquery-1.11.2.min.js" type="text/javascript" ></script>
<script type="text/javascript">

var userid= _spPageContextInfo.userId;
var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/web/getuserbyid(" + userid + ")";
var requestHeaders = { "accept" : "application/json;odata=verbose" };
$.ajax({
    url : requestUri,
    contentType : "application/json;odata=verbose",
    headers : requestHeaders,
    success : onSuccess,
    error : onError
});

function onSuccess(data, request){
    var loginName = data.d.LoginName;
    var input = loginName;
 var x = input.match(/[^|]*\|([^\\]*)\\(.*)/)
 var user = x[2];
 var domain = x[1];
    alert(loginName);
 alert(user);

 alert(domain);

}

function onError(error) {
    alert("error");
}


</script>

Open in new window


Can you please help me combine this code?

Thank you.
Price Your IT Services for Profit

Managed service contracts are great - when they're making you money. Yes, you’re getting paid monthly, but is it actually profitable? Learn to calculate your hourly overhead burden so you can master your IT services pricing strategy.

leakim971PluritechnicianCommented:
var userid= _spPageContextInfo.userId;
var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/web/getuserbyid(" + userid + ")";
var requestHeaders = { "accept" : "application/json;odata=verbose" };
$.ajax({
    url : requestUri,
    contentType : "application/json;odata=verbose",
    headers : requestHeaders,
    success : onSuccess,
    error : onError
});

function onSuccess(data, request){

    var loginName = data.d.LoginName;
    var input = loginName;
    var x = input.match(/[^|]*\|([^\\]*)\\(.*)/)
    var user = x[2];
    var domain = x[1];

    var setData = { 'migratedSecurityGroup':'Group1', 'User':user, 'domain': domain };
    $.ajax({
        type: "POST",
        url: _spPageContextInfo.webAbsoluteUrl+"/_layouts/15/FindUserSecGroupMembership/CheckUserADMembership.aspx/IsMemberOf",
        data: JSON.stringify(setData),
        async: false,
        contentType: 'application/json; charset=utf-8',
        success: function(data) {
            //debugger;
            //if(data.d.length>0){
            console.log(data)
            alert(data);
            //}
        },
        error: function (error) {debugger;
            console.log(JSON.stringify(error));
        }
    });

}

function onError(error) {
    alert("error");
}

Open in new window

sharepoint0520Author Commented:
Hi,
 Looks like it's not working. Following message display.
Message
I need to get the Boolean result and as per result "True" and "False" i need to display following
 Html.docxMessage.
leakim971PluritechnicianCommented:
I think we're on something else right?
your question was about retrieving user and domain
sharepoint0520Author Commented:
Oh Yes. Sorry for that. I will crate new one. Thank you for your help.
leakim971PluritechnicianCommented:
thank you, could you close this one?
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.