2

I expected to find an answer to a typical problem with regular expressions.

  • Find a given line
  • Change something on that line

Many examples of how to use RegEx replace assume you know the pattern you want to replace. Great for teaching but not typical (IMO). See w3 schools and MDN. If it is on the MDN page please point it out.

In my latest problem, I have an html file that has uniq IDs on a line so I can perform this simple replacement. The syntax of the lines I need modified are shown below. I can place a unique string in the file so I can find the correct line to update.

<span id="lastModified">2018-10-26</span>
<span id="builtBy">Pat</span>

I need to find the line that has id="lastModified" and change the date.

I need to find the line that has id="builtBy" and change the date.

I tried a few different things. One is

var res = str.replace(/id="lastModified".*>(.*)<.span>/, newDate );

I thought the default might be to change the data matched in the parens (nope it doesn't work that way). See JSFiddle code example of what I tried that didn't work.

NOTE: In my case I'm trying to figure this out because for a simple string replacement using gulp-replace but it's really a javascript pattern matching and replacement problem so I'm not bring gulp-replace into this question other than to mention it.

NOTE: In my case, this code is not executing in the browser, so typical DOM manipulation will not work.

Other questions that I found (but didn't answer my question) were:

7
  • 3
    In general, better to use DOM traversal when possible instead of regex. Commented Oct 26, 2018 at 14:55
  • Also, if you want to use a regular expression, pass a regular expression to .replace, not a string Commented Oct 26, 2018 at 14:57
  • 1
    (?!lastModified)[^\>]+(?=\<) this will match the date, and then, you can change it. (Works on regex101 tho: regex101.com/r/kA2kAG/1) Commented Oct 26, 2018 at 14:57
  • what's your execution environment? - describe what hosts the code need to replace the element's value (e.g. code will run inside gulp / webpack / 'browser') Commented Oct 26, 2018 at 15:01
  • @CertainPerformance, This is part of a build process not running in browser so DOM is not available in node (using gulp). Commented Oct 26, 2018 at 15:03

1 Answer 1

1

You can use this regex to match your first line:

/(<span id="lastModified">)(.*?)(</span>)/

It basically matches literally the text and captures it in three Groups, which is then used in the replacement pattern, which can be:

$12018-10-26$3

First it replaces with $1 (the first matched Group), then your date (which you will want to come from a variable) and finally $3 which is the end span.

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

1 Comment

Thanks @Poul Bak, this is the answer I came up. So the final code is var res = str.replace(/(<span id="lastModified">)(.*?)(</span>)/, '$12018-1026$3');

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.