2

I'm trying to extract a substring from a file with Javascript Regex. Here is a slice from the file :

Version=2

Format=jpg

Size=1280x960

Date=2013/05/08

Time=23:49:40

Value=250000

I want to extract only Version and Value from the text file. I tried extracting the Version using this but it doesn't return anything.

$('.Content').html().match(/^Version\:(.*)$/g);

$('.Content').html() contains the whole text file.

4
  • Because you have : in place of =? Commented Jun 15, 2015 at 18:50
  • $('.imageContent').html().match(/^Version\=(.*)$/g); is returning null too Commented Jun 15, 2015 at 18:51
  • 2
    Remove the anchors.. $('.Content').html().match(/Version=(.*)/g); Commented Jun 15, 2015 at 18:52
  • it works. how can I append the other results like I want value and version both . can I append those and then display them separated by new line ? Commented Jun 15, 2015 at 18:54

6 Answers 6

1

You have to remove the anchors or use m flag:

$('.Content').html().match(/Version=(.*)/g);

Or

$('.Content').html().match(/^Version=(.*)$/gm);

Edit: For capturing Value and Version you can do the following:

$('.Content').html().match(/Version=(.*)|Value=(.*)/g);

You will get Version in $1 and Value in $2

See DEMO

Sign up to request clarification or add additional context in comments.

6 Comments

Why are you using g flag?
@naomik Because OP has many Version's to match... In his first line he said Here is a slice from the file :)
yes you are right Karthik :) is there any chance I can merge all the result and then display them together ?
@PSDebugger no sure what you mean by merging all results.. check the update :)
yes this works and last thing how can I separate each of them by a new line.
|
1

It returns nothing because you're using : in your regex instead of =.

1 Comment

$('.imageContent').html().match(/^Version\=(.*)$/g); is returning null too
1

If you only need the Version, there's plenty of other answers here.

If you need to parse the entire file, maybe use something like this

var re = /^(\w+)=(.*)$/gm;
var result = {};
var match;

while (match = re.exec(str)) {
  result[match[1]] = match[2];
}

console.log(result.Version);
//=> "2"

console.log(result.Value);
//=> "250000"

console.log(JSON.stringify(result));
// {
//   "Version": "2",
//   "Format": "jpg",
//   "Size": "1280x960",
//   "Date": "2013/05/08",
//   "Time": "23:49:40",
//   "Value": "250000"
// }

Comments

0

You can use this regex:

/(Version|Value)=(.*)/gm

Comments

0

Your regex pattern should be: (replace : by = and remove $ and g at the end)

/^Version=(.*)/

7 Comments

Why are you using g flag?
He used that flag. I did not know about his intention so I kept it there.
Well he told you his intention was to capture the value after Version.
Yes, he will be able to do this with the second capture group. The g does not make any difference in this case.
If it makes no difference, then why is it there? Write less code™
|
0

Change : for = and add m flag.

^(?:Version|Value)=(.*)$/gm

Demo:

$( document ).ready(function() {
  var re = /^(?:Version|Value)=(.*)$/gm; 
  var str = $('.Content').html();
  var m;
  var result = "";
 
  while ((m = re.exec(str)) !== null) {
      if (m.index === re.lastIndex) {
          re.lastIndex++;
      }
    result += m[1] + ", ";
    
}
  $('#result').text(result);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="Content">
Version=2

Format=jpg

Size=1280x960

Date=2013/05/08

Time=23:49:40

Value=250000
</div>
<br>
<div id="result">test</div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.