5 Replies - 1927 Views - Last Post: 24 October 2012 - 01:09 PM

#1 adn258  Icon User is offline

  • D.I.C Addict

Reputation: 11
  • View blog
  • Posts: 761
  • Joined: 31-August 11

How Do You Replace Only Parts Of A Pattern In Regex In Javascript?

Posted 21 October 2012 - 03:17 PM

So I sort of get regex but one thing I've never gotten is replacing just a part of a string in a patter for example lets say we are looking at strings like this
name=bob occupation=construction location=washington

etc. etc.

Now lets say you want to create a regex pattern in Javascript that looks for these patterns and these are all in a list somewhere in the format above (I'm just using this as an example).

Now say you want to change the occupation on ALL of the pattern matches to "labor" WITHOUT changing the rest of the string name=xyz location=xyz etc. etc. you just want to match that pattern and change one PART of the string in the pattern. How do you go about doing that? What symbol etc. does this?

Is This A Good Question/Topic? 0
  • +

Replies To: How Do You Replace Only Parts Of A Pattern In Regex In Javascript?

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3481
  • View blog
  • Posts: 10,033
  • Joined: 08-June 10

Re: How Do You Replace Only Parts Of A Pattern In Regex In Javascript?

Posted 21 October 2012 - 10:32 PM

see String.replace(). there you can replace parts of the match.
Was This Post Helpful? 1
  • +
  • -

#3 adn258  Icon User is offline

  • D.I.C Addict

Reputation: 11
  • View blog
  • Posts: 761
  • Joined: 31-August 11

Re: How Do You Replace Only Parts Of A Pattern In Regex In Javascript?

Posted 23 October 2012 - 01:53 AM

View PostDormilich, on 21 October 2012 - 10:32 PM, said:

see String.replace(). there you can replace parts of the match.



Maybe between regex and replace I can change the string to the string I want...it's complicated and I'll have to think about it sober minded lol. Thanks though man. If you know a regex way or anyone else does though let me know
Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3481
  • View blog
  • Posts: 10,033
  • Joined: 08-June 10

Re: How Do You Replace Only Parts Of A Pattern In Regex In Javascript?

Posted 23 October 2012 - 06:56 AM

probably something along
// untested
var new_string = some_string.replace(/number=(\d+) string=(\w+)/g, "number=$1 string=foo");

Was This Post Helpful? 1
  • +
  • -

#5 adn258  Icon User is offline

  • D.I.C Addict

Reputation: 11
  • View blog
  • Posts: 761
  • Joined: 31-August 11

Re: How Do You Replace Only Parts Of A Pattern In Regex In Javascript?

Posted 24 October 2012 - 01:04 PM

View PostDormilich, on 23 October 2012 - 06:56 AM, said:

probably something along
// untested
var new_string = some_string.replace(/number=(\d+) string=(\w+)/g, "number=$1 string=foo");



Yes that's what I was looking for thanks friend!!
Was This Post Helpful? 0
  • +
  • -

#6 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3481
  • View blog
  • Posts: 10,033
  • Joined: 08-June 10

Re: How Do You Replace Only Parts Of A Pattern In Regex In Javascript?

Posted 24 October 2012 - 01:09 PM

well, that was more or less one of the examples from MDN.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1