How to retrieve  text from string in javascript?

sharepoint0520
sharepoint0520 used Ask the Experts™
on
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

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Ryan ChongSoftware Team Lead

Commented:
<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

Multitechnician
Top Expert 2014
Commented:
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

Author

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.
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

leakim971Multitechnician
Top Expert 2014

Commented:
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

Author

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.
leakim971Multitechnician
Top Expert 2014

Commented:
I think we're on something else right?
your question was about retrieving user and domain

Author

Commented:
Oh Yes. Sorry for that. I will crate new one. Thank you for your help.
leakim971Multitechnician
Top Expert 2014

Commented:
thank you, could you close this one?

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial