1

I have data with the string "\\" indicating empty or unknown date fields. I am new to regular expressions, so forgive my possible ignorance. I understand \ is the escape character, which tells the regex interpreter to ignore the following character.

So, you can match a single backslash with "\\". However, matching two consecutive backslashes has proved much more troublesome. "\\" obviously does not work, it matches one backslash and escapes the following character.

I can't use .replace() because it runs into the same problem of escaping characters, and how exactly to search for "\\" pro-grammatically has eluded me.

Google has not been useful. How do I match "\\" with regular expressions or simple javascript?

AddWeb Solution Pvt Ltd
  • 21,025
  • 5
  • 26
  • 57
awimley
  • 692
  • 1
  • 9
  • 29
  • 1
    Try using 4 of them. – Daniel A. White Aug 06 '15 at 13:09
  • Is the value of the string you want to replace ``\\``, or is the value of the string you want to replace actually ``\``, with the escape character included within your post as `"\\"`? – zzzzBov Aug 06 '15 at 13:09
  • Use this: `str.match(/\\\\/g);` where str is the string you want to search or use the `replace` function, it's up to you but four `\` characters will work. The first one escapes the second one, and the third one escapes the fourth one – ctwheels Aug 06 '15 at 13:09
  • It is actually two backslashes. Using four backslashes does not match a double backslash, as tested in javascript and http://www.regexr.com/. – awimley Aug 06 '15 at 13:17
  • @awimley `/\\\\/.test('\\\\')` works correctly, which is to say the regex matches two backslash characters. It sounds to me you're mistaking `"\\"` in a string literal for two backslash characters, when it is, in fact, a single backslash character preceded by an escape character that happens to be a backslash. – zzzzBov Aug 06 '15 at 13:30

5 Answers5

4

One slash: \\.

Two slashes: \\\\.

Four slashes: \\\\\\\\.

it matches one backslash and escapes the following character: That's not what's going on. To match a single backslash, the first backslash is needed to tell the regex "The next character is not some regex macro; I just want to match the pure character, and that character only." It's nothing to do with "skipping", or "allowing any" character. In fact, even for other characters, it would still be a backslash used to escape it.

Three asterisks: \*\*\*

Alternatively, you can write something like this.

18 backslashes: \\{18}

Katana314
  • 8,429
  • 2
  • 28
  • 36
1

Escape each \ with \\:

alert("\\\\".indexOf("\\\\")); // 0
alert((String.fromCharCode(92) + String.fromCharCode(92)).indexOf(String.fromCharCode(92) + String.fromCharCode(92))); // 0, BTW I would do this way ;)

alert(/\\\\/.test("\\\\")); // true
alert(/\\{2}/.test("\\\\")); // true
Matías Fidemraizer
  • 63,804
  • 18
  • 124
  • 206
1

If the string literal you have is "\\" and you want to match the characters within that string value, you only need to match \ because the value of the string is \.

If the string literal you have is "\\\\", you need to match \\ because the value of the string is \\.

With a regular expression literal you'd have to escape the backslash just like you would within a string literal:

/\\/g //matches any single backslash character

/\\\\/g //matches any double backslash characters

With a regular expression string passed to the RegExp constructor, you'd have to escape the backslash for the string, and then again for the regular expression:

new RegExp("\\\\", 'g'); //matches any single backslash character

new RegExp("\\\\\\\\", 'g'); //matches any double backslash characters

Strings passed to the regular expression constructor need to have a value identical to what you'd use in a regular expression literal. This means an extra level of escaping certain characters. This can become especially confusing with some characters that need to be escaped for the string, but not the regular expression value, and other characters that need to be escaped for the regular expression but not the string literal.

For example, a quote character (" or ') within a string needs to be escaped otherwise it'll end the string literal:

/"/ //regular expression to match a double quote
new RegExp("\""); //regular expression string to match a double quote character
new RegExp('"'); //alternative regular expression string to match a double quote character

Note how the backslash doesn't need to be double escaped.

As another example a . character needs to be escaped within a regular expression otherwise it'll match any character that isn't a line terminator:

/\./ //regular expression to match a period character
new RegExp('\\.'); //regular expression string  to match a period character

Note how the backslash character does need to be double escaped.

zzzzBov
  • 174,988
  • 54
  • 320
  • 367
  • Thank you, the difference between a literal and a regular expression string eluded me. This is the best answer. – awimley Aug 06 '15 at 13:53
0

Just use 4 of the backslashes \\\\.

You can see the demo here on Regex101

Asunez
  • 2,327
  • 1
  • 23
  • 46
0

$("#search").keyup(function() {
  _this = this;
  $.each($(".newgrid tbody").find("tr"), function() {
    if ($(this).text().toLowerCase().replace(/\s+/g, '').indexOf($(_this).val().replace(/\s+/g, '').toLowerCase()) == -1)
      $(this).hide();
    else
      $(this).show();
  });
  ChangeTableCount();
});

function ChangeTableCount() {
  var rowCount = $('.newgrid tr:visible').length;
  $('.newgrid caption').text("SMSF REGISTERS (TOTAL: " + (rowCount - 1) + ")");
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<input type="text" id="search" class="sb_input" placeholder="Type to search..." style="width: 532px; min-width: 100px; height: 35px; display: inline" />
<div style="margin-left: 20px;">
  <table class="newgrid">
    <thead>
      <tr class="newgrid-header">
        <th scope="col">Summary</th>
        <th scope="col">Name</th>
        <th scope="col">test</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>\\</td>
        <td>Individual</td>
        <td>Current</td>
      </tr>
      <tr>
        <td>//</td>
        <td>xyz</td>
        <td>Current</td>
      </tr>
      <tr>
        <td>145</td>
        <td>abc</td>
        <td>register</td>
      </tr>
      <tr>
        <td>girls</td>
        <td>f</td>
        <td>register4</td>
      </tr>
    </tbody>
  </table>
  <br />
</div>
showdev
  • 28,454
  • 37
  • 55
  • 73
  • 1
    While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value. – JAL Aug 06 '15 at 16:13