Search for backspace in string

I am trying to search for the character “\” in a string. From what I could find by internet search is to use 2 “\\” successive characters but I can not get that to work.

The text I am searching is “SYS-16\M1”

I am trying to drop the “SYS-16\” in front of the string. My code is shown below:

body.Name = body.Name.substring(\\)+1,body.Name.length);`

I have tried a bunch of options \\ "\\" "\" and none seem to work.

I cheated and used

body.Name = body.Name.substring(,body.Name.length);

just to complete the task but I want a more flexible code for future use.

Do I understand you correctly that you want to put SYS-16 in front of \M1?

Personally, I would use the String.replace() method to do it. And to match the backslash in a regex you just escape it with another backslash (e.g. /\\/).

I am trying to remove the “SYS-16” from the string. The characterters before the \ can vary so I want to search for the \ and remove all characters before and including the \

So anything before and including the \ should be removed if following by M1? Maybe you can give a few examples of strings and what you expect them to look like after they have been modified.

Ahh, that’s even simpler than what I thought you wanted to do and you can still do it using replace().

If you are only trying to remove the “SYS-16” from the string, then you can use the replace method as suggested by @bbsmooth.

Thanks for the replies. I don’t know java script at all and trying to edit and existing script. So I am not familiar with the correct syntax. The replace looks like a good function. I thought I tried the /\/ search string but it crashed. So what would syntax of the code look like? Would it be,\\/)+1,body.Name.length));

I am sure that is overly complicated but I need to change the Name property of the object body.

Some examples would be:
Change SYS-16\M1 to M1
Change Component\J3 to J3
Change SYS-9\Valve to Valve

OK, now we have a better understanding of what you are trying to do. So you’ll need to come up with a regex pattern that captures all the different variations on this that are possible. Other than that it is a string, I don’t know what body.Name could contain, so it would be hard for me to give you a perfect solution. For example, would it ever contain more than one backslash? Would it contain multiple words separated by spaces? Would there ever be a backslash that you wouldn’t want to make this change to? You’ll need to figure out what the requirements are and then create the appropriate regex pattern to pass into replace(). And it could involve even more than a simple replace. You might need other logic/functionality to make it work correctly, it just depends on what your requirements are.

Then, I suggest you start with the Basic JavaScript section of the FCC curriculum. It will cover all the basics and even teaches how to use regular expressions with the replace method.

Thanks for the replies. I never really thought about all the possibilities for how many combinations of backspace characters could be included. From what I have seen it is probably only one. But if there are more than it should remove from the last one. For now I will assume one and just run the code multiple times if there are more. I don’t think there will be other special characters so finding the backslash should be enough.

When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Note: Backticks are not single quotes.


Thanks for the tip. Edited my posts and it worked great.

very good thing there :+1:t2: