1

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("&nbsp;", "").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("&nbsp;", "").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>
Alexander Dixon
  • 837
  • 1
  • 9
  • 24
  • I'm inclined to close this as a duplicate of http://stackoverflow.com/q/1661197/497418. – zzzzBov Jun 10 '16 at 16:51
  • The post you reference is useful, but it does not really get me to my end goal nor does it specifically touch on the dynamic aspect of my intended results. You cannot really tie into the function that Mathias created and invoke it whilst building up and iterating over a list of varying sizes. However, thank you for the insight. – Alexander Dixon Jun 10 '16 at 17:42

0 Answers0