replace


JavaSript: Remove all non printable and all non ASCII characters from text 1

According to the ASCII character encoding, there are 95 printable characters in total.
Those characters are in the range [0x20 to 0x7E] ([32 to 126] in decimal) and they represent letters, digits, punctuation marks, and a few miscellaneous symbols.
Character 0x20 (or 32 in decimal) is the space character ' ' and
character 0x7E (or 126 in decimal) is the tilde character '~'.
Source: https://en.wikipedia.org/wiki/ASCII#Printable_characters

Since all the printable characters of ASCII are conveniently in one continuous range, we used the following to filter all other characters out of our string in JavaScript.


printable_ASCII_only_string = input_string.replace(/[^ -~]+/g, "");

What the above code does is that it passes the input string through a regular expression which will match all characters out of the printable range and replace them with nothing (hence, delete them).
In case you do not like writing your regular expression with the space character to it, you can re-write the above regular expression using the hex values of the two characters as follows:


printable_ASCII_only_string = input_string.replace(/[^\x20-\x7E]+/g, "");


Excel / Calc: Convert a Hexadecimal number to Decimal

The following examples allow you to convert hexadecimal values of the format 0xYYYYYY to decimal using a spreadsheet editor like Calc or Excel.

The following codes will remove the first two characters (the value 0x) of the cell B2 and then convert the result to decimal using the HEX2DEC function.

Using the RIGHT function

In this example, we used the RIGHT function with the num_chars parameter to be equal to the number of characters in the cell minus 2. This used to delete the 0x value from the HEX column by removing the first two characters of the cell.
To get the number of characters in the cell we use the LEN function on the cell of interest.

=HEX2DEC(RIGHT(B2,LEN(B2)-2))

Using the SUBSTITUTE function

In the following example we used the SUBSTITUTE function to automatically find the 0x prefix of the HEX value and delete it by replacing it with an empty string.

=HEX2DEC(SUBSTITUTE(B2,"0x",""))

Using the REPLACE function

The last example uses the REPLACE function. Starting from the character in position 1 in the cell, it replaces the sub-string of size 2 with the empty string and thus deleting the prefix. Please note that this function is not zero-based so the first character is at position 1 and not at position 0.

=HEX2DEC(REPLACE(B2,1,2,""))

Functions Legend:

  • RIGHT(text,[num_chars])RIGHT returns the last character or characters in a text string, based on the number of characters you specify in the variable num_chars. RIGHT always counts each character, whether single-byte or double-byte, as 1, no matter what the default language setting is.
  • LEN(text)LEN returns the number of characters in a text string. Again, LEN always counts each character, whether single-byte or double-byte, as 1, no matter what the default language setting is.
  • HEX2DEC(number)HEX2DEC converts a hexadecimal number to decimal.
  • SUBSTITUTE(text, old_text, new_text, [instance_num]) – Substitutes new_text for old_text in a text string. You can use SUBSTITUTE when you want to replace specific text in a text string.
  • REPLACE(old_text, start_num, num_chars, new_text)REPLACE replaces part of a text string, based on the number of characters you specify, with a different text string. Use REPLACE when you want to replace any text that occurs in a specific location in a text string. REPLACE always counts each character, whether single-byte or double-byte, as 1, no matter what the default language setting is.

Replace a character in all filenames

The following command will search for files in the current directory (.) that have in their name the colon character :.
The files that match will then be renamed and all instances of the colon character : in the names will be replaced by the full stop character ..

find . -name "*:*" -execdir sh -c 'mv "$1" "${1//:/.}"' _ {} \;
  • -execdir command {} + is like -exec, but the specified command is run from the subdirectory containing the matched file, which is not normally the directory in which you started find.

Example: if you have a file named 2017-03-15 14:34:44.116002523.png then it will be renamed to 2017-03-15 14.34.44.116002523.png.


NotePad++: Remove multi-line (/**/) comments automatically 4

We are going to use NotePad++ replace functionality (CTRL+H) to remove all multi-line comments that are enclosed in a pair of /* and */.

While in the source file you want to edit, open up the ‘Replace’ window either from the ‘Search’ menu or by using CTRL+H.

Set ‘Search Mode’ to ‘Regular expression’ and tick the ‘. matches newline’.

In the ‘Find what:’ input type /\*(.*?)\*/\n and leave the ‘Replace with:’ input empty.

NotePad++ Remove Multi-Line CommentsTo find the next comment, you can click the ‘Find Next’ button and then hit ‘Replace’.

To make the change on all comments just hit ‘Replace All’.