简体   繁体   中英

Text between substring and first dot

I have a text, and I need to replace some text within the original one. So, for examample:

Original Text: Flat sold with annexes. Transaction date 01-01-2014 carried out in same area. INMACULADA, 10 . Construction year: 1974. Adjustments made for hpi (4%), area (-5.00%). Closed price after adjustments: 1,022.07 €/m2.

I need to find replace what's between the substring "Construction year:" and the next dot (.) after this substring.

Final Expected Text: Flat sold with annexes. Transaction date 01-01-2014 carried out in same area. INMACULADA, 10 . New construction year: 2010. Adjustments made for hpi (4%), area (-5.00%). Closed price after adjustments: 1,022.07 €/m2.

Incorrect Final Text I´m getting I am getting the last dot (.), nor the next one, so everything between the substring and the last dot (.) is changed. Obtaining this:

Flat sold with annexes. Transaction date 01-01-2014 carried out in same area. INMACULADA, 10 . New construction year: 2010.

var new_generated_comparable_comment = generated_comparable_comment.replace(/(Catastral Reference: ). .([^.] )/, 'Catastral Reference: ' + value + ').');

Thank you.

The regex you used (Construction year:).*.([^.]*) captures Construction year: in a capturing group. Then the .* matches any character except a newline 0+ times and will match until the end of the string. After that it matches 1 time any character and 0+ times not a dot so all after Construction year: will stay being matched.

If you want to replace the year after Construction year: , you could use the capturing group $1 followed by what you want to replace the match with. Following the capturing group, you could match not a dot [^.]+ .

(Construction year: )[^.]+

Regex demo

You could add the global flag /g at the end of the regex for multiple matches.

 const regex = /(Construction year: )[^.]+/; const str = `Flat sold with annexes. Transaction date 01-01-2014 carried out in same area. INMACULADA, 10 . Construction year: 1974. Adjustments made for hpi (4%), area (-5.00%). Closed price after adjustments: 1,022.07 €/m2.`; const subst = `$12010`; const result = str.replace(regex, subst); console.log(result); 

something like this should work

  var original = 'Flat sold with annexes. Transaction date 01-01-2014 carried out in same area. INMACULADA, 10 . Construction year: 1974. Adjustments made for hpi (4%), area (-5.00%). Closed price after adjustments: 1,022.07 €/m2.' var newYear = 2010 var updated = original.replace(/Construction year: \\d{4}./, `Construction year: ${newYear}`) console.log(updated) 

With help of replace you can do like this

 let str =`Flat sold with annexes. Transaction date 01-01-2014 carried out in same area. INMACULADA, 10 . Construction year: 1974. Adjustments made for hpi (4%), area (-5.00%). Closed price after adjustments: 1,022.07 €/m2.` let op = str.replace(/(Construction year: )([^.]+)/g,"$1"+"2010" ) console.log(op) 

听起来是将String.replace()与某些Regex结合使用的好时机。

Quite easy mate, just use replace with the simple regex /Construction year:.*?\\./ ! You will catch everything from Construction year: to the next dot, whatever this is!

 const text = 'Flat sold with annexes. Transaction date 01-01-2014 carried out in same area. INMACULADA, 10 . Construction year: 1974. Adjustments made for hpi (4%), area (-5.00%). Closed price after adjustments: 1,022.07 €/m2.' function replace(year) { return text.replace(/Construction year:.*?\\./, 'New construction year: '+year+'.') } console.log(replace('2017')); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM