WhoAmI
WhoAmI

Reputation: 43

Javascript Extract from substring regex

I don't have any experience with Regex. How do I extract the value of FieldInternalName from a string like below? Need only the value without quotes.

var idString = '<!--  FieldName="App" FieldInternalName="Application" FieldType="SPFieldLookup" -->';

Upvotes: 0

Views: 90

Answers (2)

Sam
Sam

Reputation: 20486

It is very easy to set up a simple expression that will "capture" the contents of FieldInternalName:

<!--.*?\bFieldInternalName="([^"]+)".*?-->

Demo


This expression works by looking for:

  • <!--
  • 0+ characters (.*?)
  • FieldInternalName=" (preceded by a word boundary,\b, so that you don't match something like FakeFieldInternalName=")
  • 1+ non-" characters ([^"]+)
  • "
  • 0+ characters (.*?)
  • -->

Using RegExp.prototype.exec(), we can extract the contents of this match:

var idRegex = /<!--.*?\bFieldInternalName="([^"]+)".*?-->/,
    idString = '<!--  FieldName="App" FieldInternalName="Application" FieldType="SPFieldLookup" -->';

var matches = idRegex.exec(idString);
console.log(matches[1]);
<script src="https://getfirebug.com/firebug-lite-debug.js"></script>

Upvotes: 0

WinnieThePooh
WinnieThePooh

Reputation: 126

would something like this work for ya?

var idString = '<!--  FieldName="App" FieldInternalName="Application" FieldType="SPFieldLookup" -->';
var fieldInternalName = idString.match(/FieldInternalName="(.*?)"/i)[1];
alert(fieldInternalName);

Upvotes: 2

Related Questions