The CreatorCon Call for Content is officially open! Get started here.

Need help with JavaScript String substr().

Annie10
Tera Contributor

Hello,

I have a string contains "abc\1234"

I would like to extract from the back slash and get the number only such as "1234"

Can someone please help?  Thank you

 

1 ACCEPTED SOLUTION

@Annie10  Please continue to use double back slashes, generally \ has a special meaning example: \n is for the next line and hence scripting languages doesn't allow you to type one single backslash and to cut the meaning of backslash you have to use double slashes. 

 

Conclusion is \\ is equivalent of \ in a string.

 

Please use the same line of code and it should work fine.

 

return source.u_name.slice(source.u_name.indexOf('\\')+1);

 

Thanks!!

View solution in original post

16 REPLIES 16

@Annie10,

Can you please try this?

return source.u_name.slice(source.u_name.indexOf('\\')+1);

Please mark the appropriate response as correct answer and helpful, This may help other community users to follow correct solution.

Annie10
Tera Contributor

Hi @Arun_S1 

I was able to get it to work by using the following:

return source.u_name.toString().substr(5, 6)

 

Thank you for helping

I see that you have hardcoded the start and the end positions, what happens if the format changes a little, better use the indexOf function to identify the start parameter and the length of the string and then pass them in the sbustr function.

Annie10
Tera Contributor

Thank you for your suggestion @Arun_S1 

The original string contains only 1 back slash like this:  "abc\1234"

Your code is looking for double back slashes.  I tried to remove 1 back slash from the code, but that did not work.

return source.u_name.slice(source.u_name.indexOf('\\')+1);

Any advise?

 

 

@Annie10  Please continue to use double back slashes, generally \ has a special meaning example: \n is for the next line and hence scripting languages doesn't allow you to type one single backslash and to cut the meaning of backslash you have to use double slashes. 

 

Conclusion is \\ is equivalent of \ in a string.

 

Please use the same line of code and it should work fine.

 

return source.u_name.slice(source.u_name.indexOf('\\')+1);

 

Thanks!!