Note that there are some explanatory texts on larger screens.

plurals
  1. PORemove not alphanumeric characters from string. Having trouble with the [\] character
    text
    copied!<p>I want to convert the following string to the provided output. </p> <pre><code>Input: "\\test\red\bob\fred\new" Output: "testredbobfrednew" </code></pre> <p>I've not found any solution that will handle special characters like <code>\r</code>, <code>\n</code>, <code>\b</code>, etc.</p> <p>Basically I just want to get rid of anything that is not alphanumeric. Here is what I've tried...</p> <pre><code>Attempt 1: "\\test\red\bob\fred\new".replace(/[_\W]+/g, ""); Output 1: "testedobredew" Attempt 2: "\\test\red\bob\fred\new".replace(/['`~!@#$%^&amp;*()_|+-=?;:'",.&lt;&gt;\{\}\[\]\\\/]/gi, ""); Output 2: "testedobred [newline] ew" Attempt 3: "\\test\red\bob\fred\new".replace(/[^a-zA-Z0-9]/, ""); Output 3: "testedobred [newline] ew" Attempt 4: "\\test\red\bob\fred\new".replace(/[^a-z0-9\s]/gi, ''); Output 4: "testedobred [newline] ew" </code></pre> <p>One other attempt with multiple steps</p> <pre><code>function cleanID(id) { id = id.toUpperCase(); id = id.replace( /\t/ , "T"); id = id.replace( /\n/ , "N"); id = id.replace( /\r/ , "R"); id = id.replace( /\b/ , "B"); id = id.replace( /\f/ , "F"); return id.replace( /[^a-zA-Z0-9]/ , ""); } </code></pre> <p>with results</p> <pre><code>Attempt 1: cleanID("\\test\red\bob\fred\new"); Output 1: "BTESTREDOBFREDNEW" </code></pre> <p>Any help would be appreciated.</p> <p>Working Solution:</p> <pre><code>Final Attempt 1: return JSON.stringify("\\test\red\bob\fred\new").replace( /\W/g , ''); Output 1: "testredbobfrednew" </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload