Is there a refined and streamlined regex that will check against and replace characters within a string that are not compliant with JavaScript variable name syntax specifications. I have written the following:
jQuery
var content = $('pre:eq(0)').html();
var tmp = content.split("\n");
var validVar = [];
$.each(tmp, function(index, value) {
validVar.push(tmp[index].replace("!", "").replace("(", "").replace(",", "").replace(")", "").replace(".", "").replace("'", "").replace("&", "").replace(" ", "").replace("/", "").replace(/\s+/g, '').replace("-", ""));
});
var validName = validVar.toString().split(",").join("<br/>");
console.log(validName);
$('pre:eq(1)').html(validName);
HTML
<table>
<tbody>
<tr>
<td class='header zero'>Original</td>
<td class='header'>Variable Name</td>
</tr>
<tr>
<td><pre>Organizer
CapRate Events (CRE)
CTIA (Cellular Telephone Industries Association)
Dubai World Trade Centre (DWTC)
O'Reilly Conferences
PMI (Project Management Institute)
SYS-CON Events
The Software Engineering Institute (SEI)
World Academy of Science, Engineering and Technology</pre></td>
<td><pre class='valid'></pre></td>
</tr>
</tbody>
</table>
Specifically, the line:
validVar.push(tmp[index].replace("!", "").replace("(", "").replace(",", "").replace(")", "").replace(".", "").replace("'", "").replace("&", "").replace(" ", "").replace("/", "").replace(/\s+/g, '').replace("-", ""));
should be condensed into a single function.
Additionally, if we could get the variable in a well formed format that would be ideal too, such as camel-case, or atleast having the first character always lower-case.
var content = $('pre:eq(0)').html();
var tmp = content.split("\n");
var validVar = [];
$.each(tmp, function(index, value) {
validVar.push(tmp[index].replace("!", "").replace("(", "").replace(",", "").replace(")", "").replace(".", "").replace("'", "").replace("&", "").replace(" ", "").replace("/", "").replace(/\s+/g, '').replace("-", ""));
});
var validName = validVar.toString().split(",").join("<br/>");
console.log(validName);
$('pre:eq(1)').html(validName);
pre {
color: green;
font-size: 1.13em;
font-family: "Courier New", Courier, monospace;
}
table {
border-collapse: collapse;
border: solid 1px #000;
}
td {
border: solid 1px #000;
background-color: #CCC;
text-align: left;
}
td.header {
text-align: center;
font-weight: bold;
font-family: arial;
font-size: 1.35em;
background-color: #000;
color: #FFF;
}
td.zero {
border-right: solid 1px #CCC;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<table>
<tbody>
<tr>
<td class='header zero'>Original</td>
<td class='header'>Variable Name</td>
</tr>
<tr>
<td><pre>Organizer
CapRate Events (CRE)
CTIA (Cellular Telephone Industries Association)
Dubai World Trade Centre (DWTC)
O'Reilly Conferences
PMI (Project Management Institute)
SYS-CON Events
The Software Engineering Institute (SEI)
World Academy of Science, Engineering and Technology</pre>
</td>
<td><pre class='valid'></pre>
</td>
</tr>
</tbody>
</table>