Javascript正则表达式:替换任何数字附近的字母

时间:2015-06-10 20:11:37

标签: javascript regex

我有一个字符串:

F1 F#1 F2 F#2

我想将其转换为:

f1 F1 f2 F2

f/F附近的数字可以是09

是否可以使用正则表达式执行此操作?谢谢你!

2 个答案:

答案 0 :(得分:0)

使用以下内容:

sims
$`accept.progov ~ ptot_dev+swacceptcn+(swacceptcn|coal.general)`
An object of class "sim.merMod"
Slot "fixef":
  (Intercept)   ptot_dev swacceptcn
 [1,]    71.26230 -0.5967700  -5.125157
 [2,]    72.31654 -0.3331660 -13.210371
 [3,]    72.73718 -0.3910768 -15.319903
 [4,]    68.60344 -0.5775278 -10.106682
 [5,]    70.36609 -0.3897952  -7.883180
 [6,]    70.11542 -0.3413212 -10.959867
 [7,]    73.26847 -0.4599989 -10.302523
 [8,]    73.46677 -0.4627529 -14.547429
 [9,]    69.99146 -0.5947487  -8.681075
 [10,]    71.97546 -0.4976680 -10.109415

Slot "ranef":
$coal.general
, , (Intercept)

              1            2         3          4           5           6          7          8         9
[1,] -0.3275480720 -10.93724811 12.692639  -3.727188  -0.2119881  1.63602645  1.4972587 -0.4007792  1.354840
[2,] -2.9357382258  -8.47344764  9.832591 -15.602822  -2.0867660 -3.32143496  7.1446528 -7.2902852 10.593827
[3,] -0.5738514837  -6.58777257  7.189278   3.272100  -3.7302182 -2.77115752  4.6410860 -6.9497532  7.013610
[4,]  0.0008799287  -9.42620987  7.733388  -8.888649  -2.7795506 -1.98193393 -3.1739529  2.4603618  1.307669
[5,]  1.5177874134 -10.51052960 10.816926  -4.103975  -8.2232044  0.43857146  4.5353983 -8.1371223 -5.734714
[6,]  0.3591081598  -4.71170518 11.391860 -15.928789 -10.3654403  5.13397114 -1.9557418  3.6573842  7.846707
[7,] -0.1520099025  -9.97569519  5.973820  -6.601445  -5.8213534 -5.97398796  9.1813633 12.0905868 -2.689435
[8,] -3.2966495558  -3.88700417 12.069134   3.972661  -1.3056792 -5.41674684 -0.7940412  3.3800106  6.113203
[9,]  0.9239716129  -0.03016792 -4.695256  -5.092695  -1.4194101  5.82820816  6.7456858  9.4024483  7.683213
[10,]  1.8038318596  -6.69924367  9.612527  -7.118014 -13.3545691  0.03555004  7.5745529  1.6765752  8.020667

, , swacceptcn

           1         2         3           4          5         6          7          8           9
[1,] -10.799839  7.400621  3.835463  -7.5630236  -4.112801 -1.108058  -9.648384  -1.729799  -0.5488257
[2,]  -4.962062  4.103715 11.493087   6.1079040  -4.432072  6.097044  -5.972890   5.072467  -2.7055490
[3,]  -3.831015  0.486487 13.724554 -16.0322440  -5.487974  6.453326  -1.208757  13.072152  -3.1340066
[4,]  -3.053745  8.054387 12.682886   2.8787329   3.365597  2.195597   4.271775   5.460537   2.9898383
[5,]  -8.098502  4.055499  3.944880  -3.8708456 -14.567725  3.413494 -10.604984  12.821358   7.1130135
[6,]  -6.626984  3.892675  7.205407   6.3425843   9.328326 -4.693105   5.304151  11.150812  -3.4270667
[7,] -13.920626  7.548634  9.682934  -5.3058276  -1.991851  4.429253 -16.905243 -10.927869  -2.0806977
[8,]  -3.863126  2.470756  9.284932 -20.1617879  -5.352519  8.871024  -1.122215  -1.211589  -0.1492944
[9,]  -7.229178 -5.695966 25.527378  -1.7627386  -8.622444 -2.557726  -8.459804  -7.526883  -3.7090101
[10,] -11.098350  3.598449  7.642130   0.2573062   2.701967  5.834333 -14.552764   4.590748 -12.1888232



Slot "sigma":
[1] 11.96711 11.93222 11.93597 11.35270 11.31093 11.23100 11.89647 11.62934 11.61448 11.74406

请参阅DEMO

答案 1 :(得分:0)

您可以使用反向引用来执行此操作。将括号括在正则表达式的各个部分允许您在返回函数中引用它们(按从左到右的顺序)。

以下代码的工作方式与您指定的相同,首先将实例更改为#而不是小写,然后使用第二个正则表达式替换#s。

a = "F1 F#1 F2 F#2";
b = a.replace(/([A-Za-z])([0-9])/g,function(match,$1,$2,original){return $1.toLowerCase() + $2;}).replace(/([A-Za-z])#([0-9])/g,"$1$2");
//b = f1 F1 f2 F2