There are strings: & #039; sdfUjer [ema14] XX [emb15], 43 you 56 ^ & amp; % ^ % I [emc7] & #039; [ema14], [emb15], and [emc7] are the content to be replaced. & #039; [em & #039; start with & #039;] & #039; end with a combination of letters and numbers in the middle. replace all those that match the structure with & amp; lt; a14 & amp; gt;, & amp ;... there are strings: 'sdfujer [ema14] XX [emb15], 43 you 56 ^ & % ^ % I [emc7]'
[Ema14], [emb15], and [emc7] are the content to be replaced,
'[Em', ']', with a combination of letters and numbers in the middle
Replace all and all that conform to this structure ,,
Finally, we should get the following string: 'sdfujerxx , 43 you 56 ^ & % ^ % Me ]'
---------- Update ------------
Downstairs
Wen Jing [1]: http://segmentfault.com/u/wemlin
Can be replaced correctly,
Preg_replace ('/\ [em ([a-zA-Z] + \ d +) \]/', '<$1>', $ str );
Reply content:
There are strings: 'sdfujer [ema14] XX [emb15], 43 you 56 ^ & % ^ % I [emc7]'
[Ema14], [emb15], and [emc7] are the content to be replaced,
'[Em', ']', with a combination of letters and numbers in the middle
Replace all and all that conform to this structure ,,
Finally, we should get the following string: 'sdfujerxx , 43 you 56 ^ & % ^ % Me ]'
---------- Update ------------
Downstairs
Wen Jing [1]: http://segmentfault.com/u/wemlin
Can be replaced correctly,
Preg_replace ('/\ [em ([a-zA-Z] + \ d +) \]/', '<$1>', $ str );
Lz forgive me for not using PHP for a long time. Str_replace function or something.
Considering that regular expressions are basically common. From the js perspective, let's take a look at lz.
Var str = 'sdfujer [ema14] XX [emb15], 43 you 56 ^ & % ^ % I [emc7] '; var regex =/\ [em ([a-zA-Z] + \ d +) \]/g; var result = str. replace (regex, '<$1>'); console. log (result = 'sdfujerxx
, 43 you 56 ^ & % ^ % Me
] ');
$str = "[ema14]XXX[emb15]XXX[emc77]";$pattern = '/\[em([^\]]*)\]/';$resultArr = array();$index = 0;function replaceStr ($arr) {GLOBAL $resultArr;$resultArr[] = "<" . $arr[1] .">";}preg_replace_callback($pattern, 'replaceStr', $str);$str = implode("", $resultArr);