使用split方法通过正则表达式从字符串中收集数字

时间:2018-01-16 13:37:38

标签: javascript regex

我喜欢从字符串中将数字收集到数组中。我尝试了以下方式,但没有在Javascript中获得预期的结果。我怎么能这样做?

'This is string 9, that con 9 some 12, number rally awesome 8'.split(/[^\d+]/);

1 个答案:

答案 0 :(得分:1)

我假设这是JavaScript而不是Swift。

    select 1,case when B.startdate is not null then B.startdate else  case when charindex('Z',dtstart) > 0 then  dbo.VCalendar_DateFormat(dtstart) else LEFT(dtstart,4) + '-' + substring(dtstart,5,2) + '-' +  substring(dtstart,7,2)end end as startTime,
C.ID as idressource,ltrim(rtrim((select STUFF(( select ' ' + CHAR(10) + Summary + ' ' + Description
                                    from tbIcal_data AA
                                    left join tbPatient D on (replacE(replacE(replacE(Summary,' ',''),'-',''),'_','') Collate SQL_Latin1_General_CP1253_CI_AI like '%' +replace(replace(replace(D.Prenom_SansAccent + D.Nom_SansAccent,' ',''),'-',''),'_','')  + '%' ) or  ( replacE(replacE(Summary,' ',''),'-','')   like '%'+ ltrim(rtrim(REPLACE(REPLACE(isnull(D.TelephoneDomicile,'')+isnull(D.TelephoneTravail,'')+isnull(D.TelephonePortable,''),' ',''),'-','')))  + '%' and ltrim(rtrim(REPLACE(REPLACE(isnull(D.TelephoneDomicile,'')+isnull(D.TelephoneDomicile,'')+isnull(D.TelephonePortable,''),' ',''),'-',''))) <> '')
                                    outer apply dbo.ExpandRecurrence(RRule,null,0,GETDATE(),dateadd(year,2,getdate()),case when charindex('Z',AA.dtstart) > 0 then  dbo.VCalendar_DateFormat(AA.dtstart) else LEFT(AA.dtstart,4) + '-' + substring(AA.dtstart,5,2) + '-' +  substring(AA.dtstart,7,2)end,case when charindex('Z',AA.dtEnd) > 0 then  dbo.VCalendar_DateFormat(AA.dtEnd) else LEFT(AA.dtEnd,4) + '-' + substring(AA.dtEnd,5,2) + '-' +  substring(AA.dtEnd,7,2)end ) BB
                                    where ((case when bb.startdate is not null then bb.startdate else  case when charindex('Z',AA.dtstart) > 0 then  dbo.VCalendar_DateFormat(AA.dtstart) else LEFT(AA.dtstart,4) + '-' + substring(AA.dtstart,5,2) + '-' +  substring(AA.dtstart,7,2)end end >= GETDATE()
                                            and datediff(day,case when bb.startdate is not null then bb.startdate else  case when charindex('Z',AA.dtstart) > 0 then dbo.VCalendar_DateFormat(AA.dtstart) else LEFT(AA.dtstart,4) + '-' + substring(AA.dtstart,5,2) + '-' +  substring(AA.dtstart,7,2)end end,case when bb.Enddate is not null then bb.Enddate else  case when charindex('Z',AA.dtEnd) > 0 then  dbo.VCalendar_DateFormat(AA.dtEnd) else LEFT(dtEnd,4) + '-' + substring(AA.dtEnd,5,2) + '-' +  substring(AA.dtEnd,7,2)end end  ) = 1) or (datediff(day,case when bb.startdate is not null then bb.startdate else  case when charindex('Z',AA.dtstart) > 0 then  dbo.VCalendar_DateFormat(AA.dtstart) else LEFT(AA.dtstart,4) + '-' + substring(AA.dtstart,5,2) + '-' +  substring(AA.dtstart,7,2)end end,case when bb.Enddate is not null then bb.Enddate else  case when charindex('Z',dtEnd) > 0 then  dbo.VCalendar_DateFormat(dtEnd) else LEFT(dtEnd,4) + '-' + substring(dtEnd,5,2) + '-' +  substring(dtEnd,7,2)end end  ) = 0
                                            and bb.StartDate is not null))
                                    and AA.ressource = A.ressource and D.ID is null and
                                    case when bb.startdate is not null then bb.startdate else  case when charindex('Z',AA.dtstart) > 0 then  dbo.VCalendar_DateFormat(AA.dtstart) else LEFT(AA.dtstart,4) + '-' + substring(AA.dtstart,5,2) + '-' +  substring(AA.dtstart,7,2)end end  = case when B.startdate is not null then B.startdate else  case when charindex('Z',A.dtstart) > 0 then  dbo.VCalendar_DateFormat(A.dtstart) else LEFT(A.dtstart,4) + '-' + substring(A.dtstart,5,2) + '-' +  substring(A.dtstart,7,2)end end 
                                    FOR XML PATH('')),1,1,'')AS DayMessage))) AS DayMessage
from tbIcal_data A
left join tbPatient D on (replacE(replacE(replacE(Summary,' ',''),'-',''),'_','') Collate SQL_Latin1_General_CP1253_CI_AI like '%' +replace(replace(replace(D.Prenom_SansAccent + D.Nom_SansAccent,' ',''),'-',''),'_','')  + '%' ) or  ( replacE(replacE(Summary,' ',''),'-','')   like '%'+ ltrim(rtrim(REPLACE(REPLACE(isnull(D.TelephoneDomicile,'')+isnull(D.TelephoneTravail,'')+isnull(D.TelephonePortable,''),' ',''),'-','')))  + '%' and ltrim(rtrim(REPLACE(REPLACE(isnull(D.TelephoneDomicile,'')+isnull(D.TelephoneDomicile,'')+isnull(D.TelephonePortable,''),' ',''),'-',''))) <> '')
outer apply dbo.ExpandRecurrence(RRule,null,0,GETDATE(),dateadd(year,2,getdate()),case when charindex('Z',A.dtstart) > 0 then  dbo.VCalendar_DateFormat(A.dtstart) else LEFT(A.dtstart,4) + '-' + substring(A.dtstart,5,2) + '-' +  substring(A.dtstart,7,2)end,case when charindex('Z',A.dtEnd) > 0 then  dbo.VCalendar_DateFormat(A.dtEnd) else LEFT(A.dtEnd,4) + '-' + substring(A.dtEnd,5,2) + '-' +  substring(A.dtEnd,7,2)end ) B
join tbRessource C on (C.nom = A.ressource)
where ((case when B.startdate is not null then B.startdate else  case when charindex('Z',A.dtstart) > 0 then  dbo.VCalendar_DateFormat(A.dtstart) else LEFT(A.dtstart,4) + '-' + substring(A.dtstart,5,2) + '-' +  substring(A.dtstart,7,2)end end >= GETDATE()
        and datediff(day,case when B.startdate is not null then B.startdate else  case when charindex('Z',A.dtstart) > 0 then dbo.VCalendar_DateFormat(A.dtstart) else LEFT(A.dtstart,4) + '-' + substring(A.dtstart,5,2) + '-' +  substring(A.dtstart,7,2)end end,case when B.Enddate is not null then B.Enddate else  case when charindex('Z',A.dtEnd) > 0 then  dbo.VCalendar_DateFormat(A.dtEnd) else LEFT(dtEnd,4) + '-' + substring(A.dtEnd,5,2) + '-' +  substring(A.dtEnd,7,2)end end  ) = 1) or (datediff(day,case when B.startdate is not null then B.startdate else  case when charindex('Z',A.dtstart) > 0 then  dbo.VCalendar_DateFormat(A.dtstart) else LEFT(A.dtstart,4) + '-' + substring(A.dtstart,5,2) + '-' +  substring(A.dtstart,7,2)end end,case when B.Enddate is not null then B.Enddate else  case when charindex('Z',dtEnd) > 0 then  dbo.VCalendar_DateFormat(dtEnd) else LEFT(dtEnd,4) + '-' + substring(dtEnd,5,2) + '-' +  substring(dtEnd,7,2)end end  ) = 0
        and B.StartDate is not null))
and ressource <> 'Gestion' and D.ID is null
and A.Summary is not null
group by C.ID,A.ressource,case when B.startdate is not null then B.startdate else  case when charindex('Z',A.dtstart) > 0 then  dbo.VCalendar_DateFormat(A.dtstart) else LEFT(A.dtstart,4) + '-' + substring(A.dtstart,5,2) + '-' +  substring(A.dtstart,7,2)end end 

产生

'This is string 9, that con 9 some 12, number rally awesome 8 extra'.
split(/[^\d]+/);

正如你所看到的那样,它会让你大部分时间都在那里,但是有一个可能的前导和可能的尾随空字符串。

过滤器可以解决这个问题。

[ '', '9', '9', '12', '8', '' ]

产生您正在寻找的答案。

'This is string 9, that con 9 some 12, number rally awesome 8 extra'.
split(/[^\d]+/).
filter(function(number) { return number.length > 0 });

或者如果您使用的是ES6。

[ '9', '9', '12', '8' ]